Page 1 of 1

php sessions query [Resolved]

Posted: Sun Dec 26, 2010 8:14 pm
by ConceptDestiny
Hey, I have a sessions-related problem with my index page. For some reason after I log in, it won't execute the redirect script, and show all content within isset($session_username). I've tried using Print_r ($_SESSION), and it displays the correct session in the header, however none of the content except the login form shows. :/

Here's the code:

Code: Select all

<?php


    //ob
    ob_start();

    //session
    session_start();

    //connect to database
    $error = "Problem connecting";
    mysql_connect('localhost','root','') or die($error);
    mysql_select_db('mydb') or die($error);
    
    ini_set('display_errors',0);

 
    
$username = $session_username['username'];
Print_r ($_SESSION);

if (isset($_POST['login']))
{
   //get form data
   $username = addslashes(strip_tags($_POST['username']));
   $password = (strip_tags($_POST['password']));

   if (!$username||!$password)
      echo "Enter a username and password";
   else
   {
        //log in
        $login = mysql_query("SELECT * FROM user WHERE username='$username'");
        if (mysql_num_rows($login)==0)
           echo "No such user";
        else
        {
              while ($login_row = mysql_fetch_assoc($login))
              {
         
                     //get database password
                     $password_db = $login_row['password'];
              
                     //encrypt form password
                     //$password = md5($password);       
              
                     //check password
                     if ($password!=$password_db)
                        echo "<center><br><br><br><br><br><br>Incorrect password<br><br><A href='javascript:history.go(-1)'>Back</a><br><br>";
                     else
                     {
                              //check if active
                              $active = $login_row['active'];
                              $email = $login_row['email'];
                              
                              if ($active==0)
                                 echo "<center><br><br><br><br>You haven't activated your account, please check your email ($email)<br><br>";
                              else
                                  {
                                   $_SESSION['username']=$username; //assign session
                                   header('Location: index.php'); //refresh
                                  }
                     }
         
        
              }
        }
   }
}
else
{

  if (isset($session_username))
  {
    include 'menu.php'; 
    include 'index_main.php';                                       
  }
  else
  {
   echo " ((login form))";
  }

}

?>
</div> 
<?php include 'footer.php'; ?>
I'd greatly appreciate any help you guys can offer! :)

Re: php sessions query

Posted: Sun Dec 26, 2010 8:40 pm
by Chris
Is it not supposed to be $_SESSION['username'] rather than $session_username['username']?

Re: php sessions query

Posted: Sun Dec 26, 2010 11:21 pm
by ConceptDestiny
Doh. Cheers for pointing that out Chris! My problem now is that the header(location:) isn't working for some bizzare reason. :s

Re: php sessions query

Posted: Mon Dec 27, 2010 12:17 am
by ConceptDestiny
Discovered what the problem was - was some scripts I was using in a header file which I included in index.php. All working smoothly now. :D