Jump to content

Session Variables


missyevil

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]
Link to comment
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
Link to comment
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.
Link to comment
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;
Link to comment
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??
Link to comment
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


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.