NYCPHP Meetup

NYPHP.org

[nycphp-talk] EZ Session Question...

Greg Faber greg at click3x.com
Wed Feb 11 11:24:37 EST 2004


Hi all,

I just joined this list and just began writing php. I have a problem  
with my session variable and I have no doubt it's something dumb so you  
should be able to help me out pretty quickly. And I thank you all for  
your aid.

Here's the situation:
I have a login script, a registration script and a "members only"-type  
script.
Let's start at the login page: if you are a member you enter your  
username and password and you are logged in and get a message like  
"logged in as George".
If you aren't a member you must register. Once you register  
successfully, you can go to the members only page [here's my problem]  
but it says "logged in as " and then nothing. My session variable is  
obviously registered because the script gets a true result from  
session_is_registered() but the session variable contains nothing. I  
think the issue might be in the check_valid_user() function...

thanks, Greg

Here's some script excerpts:

// registered.php

require_once("images_fns.php");
session_start();
$username = $_POST["username"];
$password = $_POST["passwd"];
$valid_user = $_SESSION["valid_user"];
if ($username && $password)
// they have just tried logging in
{
     if (login($username, $password))
     {
       // if they are in the database register the user id
       $valid_user = $username;
       session_register("valid_user");
     }
     else
     {
       // unsuccessful login
       do_html_header("Problem:");
       echo "You could not be logged in.
             You must be logged in to view this page.";
       do_html_url("login.php", "Login");
       do_html_footer();
       exit;
     }
}

do_html_header("Home");
check_valid_user();
------------------------------------------------------------------------ 
--------
//part of register_new.php

    $reg_result = register($username, $email, $passwd);
    if ($reg_result == "true")
    {
      // register session variable

      session_register('valid_user');
      $valid_user = $username;

      // provide link to members page
      do_html_header("Registration successful");
      echo "Your registration was successful.  Go to the users page to  
start uploading your files!";
      do_HTML_URL("registered.php", "Go to users page");
    }
------------------------------------------------------------------------ 
-----

// check_valid_user() function used in the registered.php script
function check_valid_user()
// see if somebody is logged in and notify them if not
{

   global $valid_user;

   if (session_is_registered("valid_user"))
   {
       echo "Logged in as $valid_user";
       echo "<br>";
   }
   else
   {
      // they are not logged in
      do_html_heading("Problem:");
      echo "You are not logged in.<br>";
      do_html_url("login.php", "Login");
      do_html_footer();
      exit;
   }
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2917 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20040211/82dd07af/attachment.bin>


More information about the talk mailing list