Planetarion Forums

Planetarion Forums (https://pirate.planetarion.com/index.php)
-   Programming and Discussion (https://pirate.planetarion.com/forumdisplay.php?f=57)
-   -   Basic PHP Help: Global Variables? (https://pirate.planetarion.com/showthread.php?t=188954)

Ragnarak 20 Dec 2005 18:40

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.

JammyJim 20 Dec 2005 23:40

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 :-(

mist 20 Dec 2005 23:57

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

SYMM 21 Dec 2005 00:04

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
}


mist 21 Dec 2005 00:05

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?

SYMM 21 Dec 2005 00:22

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 :)

mist 21 Dec 2005 00:29

Re: Basic PHP Help: Global Variables?
 
yeh, i'm not quite sure what he's wanting tbh. the whole global thing confused me mildly :)

Ragnarak 21 Dec 2005 00:47

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.

Dante Hicks 21 Dec 2005 01:19

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?

JammyJim 21 Dec 2005 01:24

Re: Basic PHP Help: Global Variables?
 
cookies?

mist 21 Dec 2005 09:47

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.

Ragnarak 21 Dec 2005 16:09

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.

mist 21 Dec 2005 16:20

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)
?>

meglamaniac 21 Dec 2005 16:58

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.

mist 21 Dec 2005 17:03

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.

meglamaniac 21 Dec 2005 17:13

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($unique1610);

?>

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).

meglamaniac 21 Dec 2005 17:29

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.

meglamaniac 21 Dec 2005 17:32

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.

mist 21 Dec 2005 17:45

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 :)

meglamaniac 21 Dec 2005 17:54

Re: Basic PHP Help: Global Variables?
 
Actually it includes an optional prefix parameter that can be a call to any function you like.

mist 21 Dec 2005 17:59

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?

meglamaniac 21 Dec 2005 18:13

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.

Ragnarak 21 Dec 2005 18:36

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 07:12.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018