PDA

Click to See Complete Forum and Search --> : Set Global Variable... SESSIONS...


damasterjo
May 5th, 2006, 02:00 PM
I want to set a variable that can be accessed anywhere, how can I set it then access it? dont want a cookie though

techwizz
May 5th, 2006, 03:59 PM
You could use sessions?

//Start the session
session_start();

session_register('var1');
session_register('var2');

Then every page you go to from there needs to have...

/Start the session
session_start();

at the top.

Another option would be declaring variables in a file named something like main.php the requiring that file in each page.

Thats what PHP-Nuke does. :D

you can read here on sessions...

http://us3.php.net/session

penagate
May 6th, 2006, 12:53 AM
Nuke is an atrocious example of PHP coding. I agree sessions are the correct way to implement this. However, don't use session_register, which is deprecated. Use instead the $_SESSION superglobal array.

session_start();

// ...

// set a session var
$_SESSION['var1'] = $var1;

// get a session var
$var1 = $_SESSION['var1'];

// unset a session var
unset($_SESSION['var1']);


If you need the user to log out, you should at this point call session_destroy(), which unsets all session variables and removes the session ID.

mar_zim
May 6th, 2006, 01:12 AM
And one thing to add before you access the session variable array in another page you should start the session first.

session_start();
$var1 = $_SESSION['var1'];

damasterjo
May 8th, 2006, 02:54 PM
Okay I got sessions to work, but when I leave the site and come back it does not remember

This a code on one page
$_SESSION['UserLoggedIn'] = mysql_result($result,$DBspot,"Name");


This is on the main page
Welcome to my website <strong><? echo $_SESSION['UserLoggedIn']; ?></strong>!

When I leave and come back this function returns false
if($_SESSION['UserLoggedIn'])

???

reeksy
May 8th, 2006, 03:04 PM
Has each user who logs in got their own ID?

If so, why not register the member ID to the session variable?

Then you can say if that session has a value then print the text, else print something else.

This should work even if you close your browser session aswell. However depending on the setup of you php.ini your session may expire after a short amount of time


if ('user has logged in code here'){
$_SESSION['UserLoggedIn'] = $memberID;
}

if (isset($_SESSION['UserLoggedIn'])){
print "Welcome to my website <strong>".$_SESSION['memberName']."</strong>";
}else{
# Login script here
}

damasterjo
May 8th, 2006, 04:29 PM
ok well i think maybe the php.ini file may be formated a certain way, how would I make it never end unless they loged out?