The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 01:16:31 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: php session vars not working  (Read 1322 times)
Phaėton
Lifer
*****
Offline Offline

Posts: 555


⎝⏠⏝⏠⎠


View Profile
« on: May 25, 2010, 01:16:09 AM »

Ive tried this on two servers and im still not seeing it just trying to
create a session and use session variables...

if everything is set up correctly, shouldnt the following work
to pass a value through the $_SESSION['foo'] variable?

 Need Help

page1.php
Code:
<?php
session_start
();
$_SESSION['foo']='test';
header("Location: /sandbox/page2.php");   
?>


page2.php
Code:
<?php
 session_start
();
 if(!isset(
$_SESSION['foo']))
   {
      echo 
' im not seein it.<br>';
      
   }  else
   {
      echo 
'ah i see, its between the double colons ::'.$_SESSION['foo'].'::<br>';
   }

?>

Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« Reply #1 on: May 25, 2010, 01:46:14 AM »

Looks fine to me, that should work.

Do you have register globals on?

Found this reference, it might help.

Code:
Please note that if you have register_globals to On, global variables associated to
$_SESSION variables are references, so this may lead to some weird situations.

<?php

session_start
();

$_SESSION['test'] = 42;
$test 43;
echo 
$_SESSION['test'];

?>


Load the page, OK it displays 42, reload the page... it displays 43.

The solution is to do this after each time you do a session_start() :

<?php

if (ini_get('register_globals'))
{
    foreach (
$_SESSION as $key=>$value)
    {
        if (isset(
$GLOBALS[$key]))
            unset(
$GLOBALS[$key]);
    }
}

?>
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!