Jump to content

[SOLVED] Help! Problem storing sessions and keeping them


Recommended Posts

Okay. I have a site - tls-3.977mb.com - that has users, and on login makes a session (after doing checks and such) that keeps the user name (then i can do an if isset whatever. I have the bit I want to protect in an include file (ill template the rest of my pages off that), the include file is also included in the login page. so when the user logs in, it redirects them to the index page. However, when they login, the bar shows the user has logged in, and that works, but when it redirects to the index, it looses the session for some reason.

 

Here is include file (bar.php):

<body link="#ffebcd" alink="#ffebcd" vlink="#ffebcd">
<?php
echo '<table width="100%" border="0"><tr><td align="left">';


ob_start();

session_start();






if (isset($_SESSION['user']))

{



echo '<font color="white">Logged in as: '.$_SESSION['user']; 
echo '</font>';


}else{


  
   
echo '<font color="white">Welcome Guest! <a href="login.php">Login</a> or <a href="register.php">Register</a>.</font>';

   }

if (isset($_COOKIE["name"]))

{



echo '<font color="white">Logged in as: '.$_COOKIE["name"]; 
echo '</font>';


}else{


  
   
echo '<font color="white">Welcome Guest! <a href="login.php">Login</a> or <a href="register.php">Register</a>.</font>';

   }




echo '</td>
<td align="right">';
echo '<font color="white">';
echo '<a href="http://tls-3.977mb.com/">Home</a> | ';
if (isset($_SESSION['user']))

{
echo '<a href="logout.php">Logout</a>';
}else{
echo '<a href="login.php">Login</a> | ';
echo '<a href="register.php">Register</a> ';
}

echo '</td>
</tr>
</table>';
echo '</font>';
?>

</body>

 

And my login.php:

<?php

include 'dbs.php';

ob_start();
session_start();

if (isset($_POST['username']) && isset($_POST['password']))
{
    //if the user has just tried to log in
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    
    $query= 'select * from users '
            ."where username='$username' "
            ."and password=md5('$password')";
    $result = mysql_query($query);
    if (mysql_num_rows($result) > 0)
    {
        //if they are in the db, register the user id
        $_SESSION['user'] = $username;
  setcookie("name", $_POST["username"]); //added cookie for the cookie. seemed to work before, but it don't now.

    }
}

?>
<html>
<title>TLS-3 Login</title>


<body  bgcolor="#000000">
<p>Hi</p>
<table width="900" align="center" border="0">
<tr>
<td width="100%" height="133">
<img src="top.gif" width="900" height="133"></td>
</tr>
<tr>
<td width="100%" height="30" bgcolor="#333333">
<font color="#FFFFFF" face="Bell MT, Bahtang, Helvetica"><? include 'bar.php'; ?></font>

</td>
</tr>
<tr>
<td width="100%" bgcolor="555565">
<table border="0">
<tr>
<td>


<font face="Arial, Helvetica"><h1>Login
<hr width="300" color="white" align="left"></h1></font><br>
<table>
<tr>
<td width="17">
&nbsp
</td>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="504" align="left">
  <tr>
   <td><img src="images/spacer.gif" width="45" height="1" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="226" height="1" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="233" height="1" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="1" height="1" border="0" alt=""></td>
  </tr>

  <tr>
   <td colspan="3"><img name="art_r1_c1" src="images/art_r1_c1.png" width="504" height="11" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="1" height="11" border="0" alt=""></td>
  </tr>
  <tr>
   <td><img name="art_r2_c1" src="images/art_r2_c1.png" width="45" height="26" border="0" alt=""></td>
   <td valign="top" bgcolor="#999999"><p style="margin:0px">
<b>
<font size="+1" face="Century Gothic, Arial, Comic Sans MS, Helvetica">Login</font></b></p></td>
   <td><img name="art_r2_c3" src="images/art_r2_c3.png" width="233" height="26" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="1" height="26" border="0" alt=""></td>
  </tr>
  <tr>
   <td><img name="art_r3_c1" src="images/art_r3_c1.png" width="45" height="17" border="0" alt=""></td>
   <td colspan="2"><img name="art_r3_c2" src="images/art_r3_c2.png" width="459" height="17" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="1" height="17" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="3" background="images/art_r4_c1.png">
<table width="464" border="0" align="center">
   <tr>

<td>
   <font face="Verdana, Helvetica, Trebuchet MS, Comic Sans MS, Arial">
<?php
  if (isset($_SESSION['user']))
  {
    //logged in OK
    echo '<br>Welcome back, '.$_SESSION['user'].'!'.' Redirecting in 5 seconds or <a href="index.php">continue</a>.';
?>
<meta http-equiv="REFRESH" content="5;url=index.php">
<?php
  }
  else
   {
       if (isset($username))
       {
           //they tried to log in and failed
           echo '<font color="grey"><i>Login failed. Please try again.</i></font><br><br>';
       }
       else
       {
           //they have not tried to log in or have not yet logged out
           echo 'If you already have a user name and password, please login below. <br /><br>';
       }
    //provide a form to log in
    echo '<form method="post" action="login.php">';
    echo '<table>';
    echo '<tr><td>User Name:</td>';
    echo '<td><input type="text" name="username"></td></tr>';
    echo '<tr><td>Password:</td>';
    echo '<td><input type="password" name="password"></td></tr>';
    echo '<tr><td colspan="2" align="center">';
    echo '<input type="submit" value="Log in"></td></tr>';
    echo '</table></form>';
echo '<font size="-3">Not registered? Register <a href="register.php">here</a>.</font>';
  }
ob_end_flush();
?>
</font>
</td>
   </tr>
   </table>
</td>
   <td><img src="images/spacer.gif" width="1" height="39" border="0" alt=""></td>
  </tr>
  <tr>
   <td colspan="3"><img name="art_r5_c1" src="images/art_r5_c1.png" width="504" height="41" border="0" alt=""></td>
   <td><img src="images/spacer.gif" width="1" height="41" border="0" alt=""></td>
  </tr>
</table></td></tr></table>
</td>
<td width="20">
&nbsp
</td>
<td width="310" align="right" valign="top">
<? include 'side.php'; ?>


</td>
</tr>
</table>


</td>

</tr>


</table>



</body>
</html>


 

When i added that cookie before, it seemed to work, but then i did something, and now it won't. Please help me!!!

Let's rephrase

 

<?php

session_start();

 

should be the first two lines on every page you want to use sessions, even before HTML.

 

If that doesn't work, check your php.ini for the session path and make sure it exists.

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.