Basic PHP Help: Global Variables?
I was hoping that someone could help with this.
For our advertising system we need to generate a unique ID when a page is loaded and use that id in various files that are loaded when the page loads. Below is how I'm creating the ID but I'm not sure where to put the code for this to work site-wide or how it needs modifying. PHP Code:
Basically I need to figure out how to use the function to create the variable when a page is loaded and be able to use it throughout various files. How does PHP declare global variables? Maybe I'm getting confused but all I see when googling is how to use a already defined global variable inside a function (global $variable;). I hope that made at least some sense and someone can help out. |
Re: Basic PHP Help: Global Variables?
i only know ASP but in that its
session("variable") = valuehere i suppose its possible it might help but probably not :-( |
Re: Basic PHP Help: Global Variables?
do you mean that you need a unique variable for each instance of the page, or for each user?
if the latter, google use cookies, so i assume it's a fairly decent solution |
Re: Basic PHP Help: Global Variables?
session_start();
at the top of every page, then to set a variable: $_SESSION['id']=OurID(); and $id=$_SESSION['id']; to access it,. the 'global' keyword isn't what you're talking about... [edit] re-reading leaves me a buit confused. The above will alow access to the variable between pages for the length of a session. For access within one page, passing it to any functions that need it is the best way, or if you really insist: Code:
$id=OurID(); |
Re: Basic PHP Help: Global Variables?
any particular reason why people are suggesting sessions?
i thought sessions were... well... single session, whereas advertising usually wants multi session ID? |
Re: Basic PHP Help: Global Variables?
that isn't how I read his question at all (either the first or the second time...!), but think I'll wait for a better explanation of the problem before any more theorising :)
|
Re: Basic PHP Help: Global Variables?
yeh, i'm not quite sure what he's wanting tbh. the whole global thing confused me mildly :)
|
Re: Basic PHP Help: Global Variables?
Sorry for the crappy explanation.
I need to create a unique ID when someone loads a page. I'm doing this via the function I posted above. The page will have two adverts on it, one in (for example) header.php and one in a footer.php. I need to be able to use the same unique ID for both adverts. How I was hoping it could be done was that header.php has the function in it to create the ID and then I can just use $OurID in the footer and it's the same value as it was in the header. The next time the page is loaded the value is changed to a new random ID. |
Re: Basic PHP Help: Global Variables?
Can't you just generate the ID in the main body of the page (each time it loads) and then just do the header & footer by a function (which you pass the ID number). So header($ID) and footer($ID)...? Just include the header and footer files wherever and then call the functions where appropriate?
|
Re: Basic PHP Help: Global Variables?
cookies?
|
Re: Basic PHP Help: Global Variables?
dante has a fair point.
that asside, you realise that random isn't the same as unique, right? also, why use pow when you could just write the numbers in there and save some processing time*? *assuming that they're not done when the script 'loads'. dunno if php has any optimising. |
Re: Basic PHP Help: Global Variables?
Quote:
Can you explain what Dante meant? I think he's suggesting what I'm trying to do but I can't get it working. An 8-digit random number is fine for what we need, so yeah, not technically unique but it's ok. |
Re: Basic PHP Help: Global Variables?
something along the lines of:
<?php $myrandom = rand(10000000,99999999); myheaderfunction($myrandom); //random stuff that your page does here myfooterfunction($myrandom) ?> |
Re: Basic PHP Help: Global Variables?
A quick and easy way to get a unique ID is to MD5 the IP address that loaded the page combined with the current timestamp. Unless two computers from the same IP load the same page in the exact same second, there will be no conflicts.
To reduce that chance you can add a random number component to the hash string as well. |
Re: Basic PHP Help: Global Variables?
Quote:
from a purely mathamatical perspective (i don't know the md5 algorithm) it would seem that there's a 1 in 2^128 chance of the number being duplicated if you're using a 128 bit md5. |
Re: Basic PHP Help: Global Variables?
Convergence is possible but very VERY unlikely. Weaknesses have been exposed in MD5 that allow convergences to be found more easily, but that isn't an issue here - that's to get around password encryption. You can't easily alter your IP address, and the other two parameters are outside of your control.
As an example: http://www.megla.net/unique.php PHP Code:
|
Re: Basic PHP Help: Global Variables?
Anyway, if you slapped all that (and took out the echos) into a seperate PHP file as a function (let's say getUid), and added "return $unique;" at the end, you could use something like this:
PHP Code:
|
Re: Basic PHP Help: Global Variables?
More updates:
I just realised PHP has a function to do this shit anyway: http://uk.php.net/uniqid Oh well. |
Re: Basic PHP Help: Global Variables?
uniqueid is time dependant tho, and you can't guarentee that you'll process the whole page in the same second, so it doesn't answer the op, really :)
|
Re: Basic PHP Help: Global Variables?
Actually it includes an optional prefix parameter that can be a call to any function you like.
|
Re: Basic PHP Help: Global Variables?
Quote:
|
Re: Basic PHP Help: Global Variables?
Well tbh I thought we were done with the "getting it available through the whole script" bit - declare it as a variable at the start and then there it is.
I was just pointing out ways to make "better" UIDs. |
Re: Basic PHP Help: Global Variables?
Thanks so much guys. I've managed to get it working now :cool:
|
All times are GMT +1. The time now is 20:55. |
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018