php sessions

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ok, ive looked on google and php.net, and i havent really been able to find wht im looking for.

you see, im tryign to write some information to the user's session (ie their username, and authorization level), but im totally at a loss as to how session work in php...

im using a '$_SESSION['username'] = $username;' but it is not there after i swich pages... and i have no idea what i am doing wrong...

could someone just give me a quick sample of code where you creat a session, write stuff to it, and then access it from another page? tyvm
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23455
  • Loc: Woodbridge VA

Post 3+ Months Ago

You forgot the OZZU search:

http://www.ozzu.com/programming-forum/user-auth-and-sessions-php-t19035.html&hi ... b+sessions

in other words:

http://www.phpbb.com/kb/article.php?article_id=54
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

i did do an ozzu search, however it came up with 747 possible matches, and the ones i looked at didnt anwser my question... :P

i read the 2 links you posted and i still dont get it... i might have missed something, but they seem to be talking more about session security, then how sessions actually work in PHP...

the only time i have ever done anything with sessions was with ASP, and with that you dont have to do anything sith session IDs, etc... you just stick session in front of it, and there ya go, its available until you set it to nothing, or close the browser window...

do you think it might be possible toshow me an example of you creating a session, storeing information into the session variables, accessing those variable from a copletely separate page, and then ending the session (ie a logout)
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

this may sound like a stupid question, but did you session_start()?

I've got some code around here somewhere - I'll have to find it though
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

I was able to locate this:
PHP Code: [ Select ]
   if(isset($action)){
 
      if(preg_match("/login/i", $action)){
 
         session_start();
 
         $adminName = $_POST['adminName'];
 
         $adminPasswd = $_POST['adminPasswd'];
 
         if( $adminName == ''){ error( "Admin name required" ); }
 
         if( $adminPasswd == ''){ error( "Admin password required" ); }
 
         if( $adminName != $ADMIN_NAME ){ error( "Invalid admin name" ); }
 
         if( $adminPasswd != $ADMIN_PASS ){ error( "Invalid password" ); }
 
         $_SESSION['adminName'] = $adminName;
 
         $_SESSION['adminPasswd'] = $adminPasswd;
 
         header( "Location: ./admin_menu.php" );
 
      } elseif( $action == "logout" ){
 
         session_start();
 
         unset( $_SESSION['adminName'] );
 
         unset( $_SESSION['adminPasswd'] );
 
         displayHeader( "Admin Logout" );
 
         echo "<p align=\"center\"><font size=\"4\">You have logged out successfully!</font></p>\n";
 
         echo "<p align=\"center\"><a href=\"index.php\"><b>Home</b></a></p>\n";
 
         displayFooter();
 
      }
  1.    if(isset($action)){
  2.  
  3.       if(preg_match("/login/i", $action)){
  4.  
  5.          session_start();
  6.  
  7.          $adminName = $_POST['adminName'];
  8.  
  9.          $adminPasswd = $_POST['adminPasswd'];
  10.  
  11.          if( $adminName == ''){ error( "Admin name required" ); }
  12.  
  13.          if( $adminPasswd == ''){ error( "Admin password required" ); }
  14.  
  15.          if( $adminName != $ADMIN_NAME ){ error( "Invalid admin name" ); }
  16.  
  17.          if( $adminPasswd != $ADMIN_PASS ){ error( "Invalid password" ); }
  18.  
  19.          $_SESSION['adminName'] = $adminName;
  20.  
  21.          $_SESSION['adminPasswd'] = $adminPasswd;
  22.  
  23.          header( "Location: ./admin_menu.php" );
  24.  
  25.       } elseif( $action == "logout" ){
  26.  
  27.          session_start();
  28.  
  29.          unset( $_SESSION['adminName'] );
  30.  
  31.          unset( $_SESSION['adminPasswd'] );
  32.  
  33.          displayHeader( "Admin Logout" );
  34.  
  35.          echo "<p align=\"center\"><font size=\"4\">You have logged out successfully!</font></p>\n";
  36.  
  37.          echo "<p align=\"center\"><a href=\"index.php\"><b>Home</b></a></p>\n";
  38.  
  39.          displayFooter();
  40.  
  41.       }


Ain't grep great
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

thanks... i have something similar to that...
PHP Code: [ Select ]
session_start();
 
         header('location: /fanfiction/manage.php');
 
         $_SESSION['username'] = $username;
 
         $_SESSION['authlevel'] = 1;
 
         $alert = 'logged in';
  1. session_start();
  2.  
  3.          header('location: /fanfiction/manage.php');
  4.  
  5.          $_SESSION['username'] = $username;
  6.  
  7.          $_SESSION['authlevel'] = 1;
  8.  
  9.          $alert = 'logged in';


and then im try to access $_SESSION['username'] and $_SESSION['authlevel'] to confirm that the use is logged in on the page 'manage.php' which the above code redirects to after you log in... and when i echo $_SESSION['username'] in 'manage.php' it is empty...
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

are you doing session_start() on the other page as well? It needs to be called on each page that uses it - at least, I think it does.

I have my own routines for this, so I don't use it very much - so I could be wrong
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

*smacks forehead*

that was it... i feel really stupid now...

thanks much
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

np, to show how bored I am this evening, I looked up your sn - Interested me in the "how'd you pick you sn post". here's what I found:

from the Latin Dictionary:
Calendae = Kalendae; q.v

kalendae (kelen-dae): sing. pres. imperative act. : (you) GO!

Thought you might find that humorous

This
  • Blinkster182
  • Beginner
  • Beginner
  • Blinkster182
  • Posts: 59

Post 3+ Months Ago

I always thought that when your using session variables you used the session_start() and when you wanted to register a variable you used session_register("var name") and then you called it as a normal variable aka:
PHP Code: [ Select ]
<?php
 
session_start()
 
session_register("username");
 
 
 
echo( "$username");
 
 
 
?>
  1. <?php
  2.  
  3. session_start()
  4.  
  5. session_register("username");
  6.  
  7.  
  8.  
  9. echo( "$username");
  10.  
  11.  
  12.  
  13. ?>
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

look at the cautions on this page:
http://us4.php.net/manual/en/function.session-register.php
  • Blinkster182
  • Beginner
  • Beginner
  • Blinkster182
  • Posts: 59

Post 3+ Months Ago

Wow thanks alot.

I didnt know about that.

Oh well back to my Sessions and changing the formatting

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 86 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.