|
-
May 5th, 2006, 02:00 PM
#1
Thread Starter
Fanatic Member
Set Global Variable... SESSIONS...
I want to set a variable that can be accessed anywhere, how can I set it then access it? dont want a cookie though
Last edited by damasterjo; May 8th, 2006 at 04:30 PM.
Software languages known:
Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
Software API's known:
Directx 7 and 8
Internet languages, in the process of learning:
HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX
-
May 5th, 2006, 03:59 PM
#2
Addicted Member
Re: Set Global Variable, not comming from a post or form
You could use sessions?
PHP Code:
//Start the session
session_start();
session_register('var1');
session_register('var2');
Then every page you go to from there needs to have...
PHP Code:
/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. 
you can read here on sessions...
http://us3.php.net/session
-
May 6th, 2006, 12:53 AM
#3
Re: Set Global Variable, not comming from a post or form
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.
PHP Code:
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.
-
May 6th, 2006, 01:12 AM
#4
Re: Set Global Variable, not comming from a post or form
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'];
-
May 8th, 2006, 02:54 PM
#5
Thread Starter
Fanatic Member
Re: Set Global Variable, not comming from a post or form
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'])
???
Software languages known:
Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
Software API's known:
Directx 7 and 8
Internet languages, in the process of learning:
HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX
-
May 8th, 2006, 03:04 PM
#6
New Member
Re: Set Global Variable, not comming from a post or form
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
PHP Code:
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
}
-
May 8th, 2006, 04:29 PM
#7
Thread Starter
Fanatic Member
Re: Set Global Variable, not comming from a post or form
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?
Software languages known:
Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
Software API's known:
Directx 7 and 8
Internet languages, in the process of learning:
HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|