Jump to content


Photo

Session Variables


  • Please log in to reply
8 replies to this topic

#1 missyevil

missyevil
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 23 March 2006 - 03:38 PM

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

<? 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';
}
?>

jshome.php

<?
session_start();

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

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

?>


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 23 March 2006 - 03:54 PM

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()?

Info: PHP Manual


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 March 2006 - 03:59 PM

In each script put this debugging line after the "session_start()" line:
<?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?>
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

#4 missyevil

missyevil
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 23 March 2006 - 04:41 PM

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.

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 23 March 2006 - 04:48 PM

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;

Info: PHP Manual


#6 missyevil

missyevil
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 23 March 2006 - 05:31 PM

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??

#7 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 23 March 2006 - 06:12 PM

Run a query to get them.....? You can run more than one query on the page.

I'm confused by the question.

Info: PHP Manual


#8 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 23 March 2006 - 06:19 PM

[!--quoteo(post=357680:date=Mar 23 2006, 05:31 PM:name=missy)--][div class=\'quotetop\']QUOTE(missy @ Mar 23 2006, 05:31 PM) View Post[/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] * FROM jslogin, otherTable WHERE jslogin.email = 'some email' AND jslogin.password = 'some password' AND [!--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



Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#9 missyevil

missyevil
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 24 March 2006 - 01:55 PM

Thanks!! I'll give it a go :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users