|
20 Dec 2005, 18:40
|
#1
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
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:
function OurID() { return rand(pow(10,7), (pow(10,8))-1); } $OurID=OurID();
We also need various conditions such as whether we're on the home page, on the forums or somewhere else to display different ads.
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.
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
20 Dec 2005, 23:40
|
#2
|
Godfather
Join Date: May 2000
Location: England
Posts: 5,185
|
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 :-(
__________________
Forum Administrator
Mail : [email protected] // IRC : #forums
__________________
It's not personal, it's just business.
|
|
|
20 Dec 2005, 23:57
|
#3
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
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
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 00:04
|
#4
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
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();
//some code that does things
makeAdvert();
///etc.
function makeAdvert() {
global $id;
///you can use $id here and changes to itwill persist
}
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
21 Dec 2005, 00:05
|
#5
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
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?
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 00:22
|
#6
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
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
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
21 Dec 2005, 00:29
|
#7
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
Re: Basic PHP Help: Global Variables?
yeh, i'm not quite sure what he's wanting tbh. the whole global thing confused me mildly
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 00:47
|
#8
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
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.
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
21 Dec 2005, 01:19
|
#9
|
Clerk
Join Date: Jun 2001
Posts: 13,940
|
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?
|
|
|
21 Dec 2005, 01:24
|
#10
|
Godfather
Join Date: May 2000
Location: England
Posts: 5,185
|
Re: Basic PHP Help: Global Variables?
cookies?
__________________
Forum Administrator
Mail : [email protected] // IRC : #forums
__________________
It's not personal, it's just business.
|
|
|
21 Dec 2005, 09:47
|
#11
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
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.
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 16:09
|
#12
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: Basic PHP Help: Global Variables?
Quote:
Originally Posted by mist
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.
|
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.
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
21 Dec 2005, 16:20
|
#13
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
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)
?>
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 16:58
|
#14
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
21 Dec 2005, 17:03
|
#15
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
Re: Basic PHP Help: Global Variables?
Quote:
Originally Posted by meglamaniac
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.
|
i'm fairly sure there are papers out there proving that two different strings can have the same MD5, so that's not neccessarily true.
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.
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 17:13
|
#16
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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:
<?php if ($_SERVER['HTTP_X_FORWARDED_FOR'] == NULL) $ip = $_SERVER['REMOTE_ADDR']; else { $source = $_SERVER['HTTP_X_FORWARDED_FOR']; $pattern = "/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/"; preg_match($pattern, $source, $matches); $ip = $matches[0]; } $timestamp = time(); $myrandom = rand(10000000,99999999); echo $ip . " + " . $timestamp . " + ". $myrandom . "<br />"; $unique = md5($ip . $timestamp . $myrandom); echo "MD5 ID (hex) = $unique <br />"; echo "MD5 ID (dec) = " . base_convert($unique, 16, 10); ?>
The stuff at the top is there to get the real IP if you are going through a proxy (and the proxy is correctly forwarding it), including a regular expression to get it out of the string. If there is nothing in the field, it takes the standard remote address to be the IP address. This may either be your IP if you're directly connected, or the IP of an anonymous proxy (one which doesn't fill in the FORWARD field).
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
Last edited by meglamaniac; 21 Dec 2005 at 17:34.
|
|
|
21 Dec 2005, 17:29
|
#17
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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:
<?php require_once("uidGenerator.php"); $uid = getUid(); include "pageHeader.php"; //stuff include "pageFooter.php"; ?>
Please note, you do NOT need to pass the variable $uid to the header/footer includes as PHP variables are global by default, so will be available to any code (included or not) from the point of their declaration.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
Last edited by meglamaniac; 21 Dec 2005 at 17:37.
|
|
|
21 Dec 2005, 17:32
|
#18
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
21 Dec 2005, 17:45
|
#19
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
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
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 17:54
|
#20
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
Re: Basic PHP Help: Global Variables?
Actually it includes an optional prefix parameter that can be a call to any function you like.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
21 Dec 2005, 17:59
|
#21
|
Jolt's best friend
Join Date: Feb 2003
Posts: 2,101
|
Re: Basic PHP Help: Global Variables?
Quote:
prefix is optional but can be useful, for instance, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond
|
seems to add some stuff to the start of the returned string. i'm not quite sure how that's useful for making sure you get the same ID throughout a script?
__________________
<Karmulian> subtle as a kick in the nuts as always
|
|
|
21 Dec 2005, 18:13
|
#22
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
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.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
21 Dec 2005, 18:36
|
#23
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: Basic PHP Help: Global Variables?
Thanks so much guys. I've managed to get it working now
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
|
All times are GMT +1. The time now is 07:52.
| |