Jump to content

[SOLVED] Session variables not carrying over


Styles2304

Recommended Posts

This code is identical to another login.php for the main website. This is a sub site if you will but for some reason, the $_SESSION variables don't carry over the way they do on the main site. I know the server has them enabled because, like I said, on the main website this works fine. If I test it using some echo's and what not . . . it definitely the information because I can spit it right back out however, it won't store the date in the database properly and won't keep the variables once the user is forwarded.

 

Here is the code:

 

<?php
//Starting the session and user authentication
session_start();
include "conn.inc.php";

if (isset($_POST['submit'])) {
  $query = "SELECT * FROM Profile " .
           "WHERE Username = '" . $_POST['username'] . "' " .
           "AND Password = '" . $_POST['password'] . "'";
  $result = mysql_query($query,$link)
    or die(mysql_error());
    
  if (mysql_num_rows($result) == 1) {
    while ($row = mysql_fetch_array($result)) {
      $_SESSION['user_index'] = $row['IndexNo'];
      $_SESSION['user_fname'] = $row['FirstName'];
      $_SESSION['user_lname'] = $row['LastName'];
    }
    
    $LastLog = date('Y-m-d');

    $query = "UPDATE Profile SET LastLog = " . $LastLog . " WHERE IndexNo = " . $_SESSION['user_index'] . "";

    mysql_query($query,$link) or die(mysql_error());

    header("Refresh: 1; URL=profile.php");
    echo "You are being logged in<br>";
    echo "(If your browser doesn't support this, <a href=\"profile.php\">click here</a>)";
  } else {
?>

<html>
<head>
<title>Zero Gravity</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<LINK REL=StyleSheet HREF="style.css" TYPE="text/css">

</head>
<body background="images/main_background.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
Invalid Username and/or Password

<br><br>

<form action="login.php" method="post">
  <input type="hidden" name="redirect"
    value="<?php echo $_POST['redirect']; ?>">
    
  Username: <input type="text" name="username"><br><br>
  Password: <input type="password" name="password"><br><br>
  <input type="submit" name="submit" value="Login">
</form>

<br>

<font size="2pt">
  If you have forgotten your password, send an e-mail to <a href="mailto:styles2304@ma.rr.com" class="white">Styles2304@ma.rr.com</a>
  <br>
  with the subject of Login Information.
</font>

</center>
</body>
</html>

<?php
  }
} else {
  if (isset($_GET['redirect'])) {
    $redirect = $_GET['redirect'];
  } else {
    $redirect = "profile.php";
  }
?>

<html>
<head>
<title>Zero Gravity</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<LINK REL=StyleSheet HREF="style.css" TYPE="text/css">

</head>
<body background="images/main_background.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
Login below by entering your username and password.

<br><br>

<form action="login.php" method="post">
  <input type="hidden" name="redirect"
    value="<?php echo $_POST['redirect']; ?>">
    
  Username: <input type="text" name="username"><br><br>
  Password: <input type="password" name="password"><br><br>
  <input type="submit" name="submit" value="Login">
</form>

<br>

<font size="2pt">
  If you have forgotten your password, send an e-mail to <a href="mailto:styles2304@ma.rr.com" class="white">Styles2304@ma.rr.com</a>
  <br>
  with the subject of Login Information.
</font>

</center>
</body>
</html>

<?php
}
?>

Link to comment
Share on other sites

Have you tried a different browser/platform? Maybe for some reason your browser is not receiving/sending the session cookie or PHP_SESSID variable.

 

Is the only place that you access the $_SESSION  variable on those 3 lines where you set the indexno, firstname and last name? You could be accidentally overwriting the $_SESSION variable contents some other place.

Link to comment
Share on other sites

dptr: I use session variables on the main website and they work fine so the browser/platform is NOT that problem. Also, there are only two pages right now, the login page and the profile page. The only place the session variables are referenced on the profile page is where I try to retrieve information from the database based on the $_SESSION['user_index']. So, they are definitely not overwritten. Another side note, the session variables can't even be echoed on the profile page so for some reason they're not getting there after the re-direct.

 

GingerRobot: Check the code, I define the session variables that I need. Your second question, no, I don't have session_start() at the top of the second page because I don't need it . . . the session has already started by that point.

 

ap_: not a free domain and this all works on the main site . . . something is messed up here.

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.