NYCPHP Meetup

NYPHP.org

[nycphp-talk] EZ Session Question...

Chris Hubbard chubbard at next-online.net
Wed Feb 11 11:56:40 EST 2004


Greg,
Couple things.
make sure session_start(); or ob_start(); is at the top of your file, 
either right after the <?php tag or after the comments at the top of the 
file (you have comments right?).
Depending on the version of PHP you've got installed and how you've got 
it configured there are multiple answers to your problem.
I'm going assume you've got a recent version of PHP and you've got it 
configured the "right" way.

at the top of every page, start with:
<?php
sesssion_start();

If you want to put some data into a session do this:
$_SESSION["key"] = "some value"; or
$_SESSION["key"] = $value;

In your case looks like you want to have:
$_SESSION["key"] = $username;
(that's assuming you've cleaned and validated $username ( - which you 
haven't in your code..)).

Then when you want to display the username:
echo "Logged in as: ". $_SESSION["username"] ."<br>";

Rather than having a "valid_user" type:  If the user is not valid, don't 
add the username to the session.  It's easier than juggling multiple 
variables that contain similar information.

I also recommend that you turn on error reporting while you code:
error_reporting("E_ALL");

Chris

Greg Faber wrote:

> 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;
> }
> }
>
>------------------------------------------------------------------------
>
>_______________________________________________
>talk mailing list
>talk at lists.nyphp.org
>http://lists.nyphp.org/mailman/listinfo/talk
>  
>



More information about the talk mailing list