Jump to content

Archived

This topic is now archived and is closed to further replies.

missyevil

Session Variables

Recommended Posts

Firstly, sorry for posting about sessions - I know there are lots already but I *have* searched, and asked friends, but can't figure this one out!!

I have a simple login system up and running, using PHP and MySQL. Data is being retrieved from the database ok, as the user can login. The problem is that the $_SESSION variables I set when they login are not being displayed in the next page, even though I am using session_start(); at the beginning of each page.

Any help would be much appreciated as this has had me stumpped for days!!

Here's the code:

login.php

[code]<? session_start();

include 'db.php';

// Convert to simple variables
$email = $_POST['email'];
$password = $_POST['password'];

if((!$email) || (!$password))
{
    echo "<strong>Please enter ALL of the information! </strong><br />";
    include 'welcome.htm';
    exit();
}

// check if the user info validates the db
$sql = "SELECT * FROM jslogin WHERE email='$email' AND password='$password'";
$qresult=mysql_query($sql);
$login_check = mysql_num_rows($qresult);

if($login_check > 0)
{
    while($row = mysql_fetch_array($qresult))
    {
      foreach( $row AS $key => $val )
      {
        $$key = stripslashes( $val );
      }
      $_SESSION[valid] = $valid;
      // Register some session variables!
      session_register('jsid');
      $_SESSION['jsid'] = $jsid;
      session_register('forename');
      $_SESSION['forename'] = $forename;
      session_register('surname');
      $_SESSION['surname'] = $surname;
      session_register('email');
      $_SESSION['email'] = $email;
      mysql_query("UPDATE jslogin SET last_jslogin=now() WHERE jsid='$jsid'");
      header("Location: jshome.php");
    }
}
else
{
    echo "<strong>You could not be logged in!<br />
    Please try again!</strong><br />";
    include 'welcome.htm';
}
?>[/code]

jshome.php

[code]<?
session_start();

echo "Welcome ". $_SESSION['forename'] ." ". $_SESSION['surname'] ."! You are logged in!<br /><br />";

echo "<br /><a href=logout.php>Logout</a>";

?>[/code]

Share this post


Link to post
Share on other sites
You don't need to use session_register().

Are you getting any errors? Is there any whitespace before you open the PHP tags before session_start()?

Share this post


Link to post
Share on other sites
In each script put this debugging line after the "session_start()" line:
[code]<?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?>[/code]
This will dump the contents of the $_SESSION array to your screen so you can see what the system thinks you stored.

Also, what web server are you using? I have heard of problems with sessions and IIS.

Ken

Share this post


Link to post
Share on other sites
No - no whitespace.

I wasn't getting any errors before. Now when I add the code suggested it shows the contents of the array, which has the jsid (primary key) and the email address in it....

And I also get this error now:

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Project\login.php:3) in C:\Program Files\Apache Group\Apache2\htdocs\Project\login.php on line 42

I'm using Apache as my webserver.

Share this post


Link to post
Share on other sites
You would get that if you output something to the browser before calling the header() function.

One other thing I noticed:

$_SESSION[valid] = $valid;

You don't have any quotes around the key name.

$_SESSION['valid'] = $valid;

Share this post


Link to post
Share on other sites
Have finally realised my mistake (and my own stupidity) - the login variables (email and password) are stored in a different table in my database that the other variables.

Can anyone suggest an easy way to get variables from other tables at the same time as a user is logging in, and assign them to $_SESSION variables??

Share this post


Link to post
Share on other sites
Run a query to get them.....? You can run more than one query on the page.

I'm confused by the question.

Share this post


Link to post
Share on other sites
[!--quoteo(post=357680:date=Mar 23 2006, 05:31 PM:name=missy)--][div class=\'quotetop\']QUOTE(missy @ Mar 23 2006, 05:31 PM) [snapback]357680[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Have finally realised my mistake (and my own stupidity) - the login variables (email and password) are stored in a different table in my database that the other variables.

Can anyone suggest an easy way to get variables from other tables at the same time as a user is logging in, and assign them to $_SESSION variables??
[/quote]

Of course.. You can use a JOIN.. or you can use a simple statement

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]jslogin,[/color] otherTable [color=green]WHERE[/color] jslogin.email [color=orange]=[/color] [color=red]'some email'[/color] [color=blue]AND[/color] jslogin.password [color=orange]=[/color] [color=red]'some password'[/color] [color=blue]AND[/color] [!--sql2--][/div][!--sql3--]

Notice the ', otherTable' obviously that's the other table where the data is stored.. I can't help anymore without knowing your db structure, but you get the hint. :D


Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.