0

I have a login.php file:

<?php

session_start();

include('db.php');

            if(isset($_POST['email']) && !empty($_POST['email']) AND isset($_POST['password']) && !empty($_POST['password'])){
                $email = mysql_escape_string($_POST['email']);
                $password = mysql_escape_string($_POST['password']);

                $search = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' AND active='1'") or die(mysql_error()); 
                $match  = mysql_num_rows($search);

                if($match > 0){
                    $row = mysql_fetch_assoc($search);
                    $user=$row['forename'].' '.$row['surname'];
                    $_SESSION['username']=$user;
                    //$msg = 'Login Complete! Thanks, '.$user.'!';
                    header( 'Location: page1.php' ) ;
                    die;
                }else{
                    $msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
                }
            }


?>

Now, when I load page1.php I have issues. The file contains this...

<?php

session_start();

include('db.php');

if(isset($_SESSION['username'])){
    echo 'Success, '.$_SESSION['username'].'!';
}else{
    echo 'No dice!';
    //header( 'Location: login.php' ) ;
}

?>

I ideally want it to redirect to login if there isn't a username stored. If there is, I want to allow them to view. However, I am getting "No dice!" every time, so it looks like I am not retrieving (or storing) the data correctly. What am I doing wrong?


To be clear, the else shouldn't be firing as it should be referring to session data set in index.php. The redirection is not a problem.


The problem was specific to my host who had a strange setup. On contacting them, they provided me the correct path information which I had to use session_save_path to set. Awarded the right answer on this basis.

3
  • What does the $search Variable Contain? or where is it defined? include more little peace of code on Querying the User data. Commented Feb 27, 2013 at 15:28
  • It's irrelvant. The check is only to see if it's set, not if it has a value. Are you starting the session in login.php? Commented Feb 27, 2013 at 15:29
  • I start session in login.php too. Should I only start it there? Is starting it again a bad thing? Commented Feb 27, 2013 at 15:32

2 Answers 2

3

Redirect is malformed. You have to specify full url, like this:

header('Location: http://your.site.com/page1.php');
die;

It's important to end the script after redirect.

If session data is not preserved, maybe you have some configuration issues. Verify your php configuration and check write permissions where session data is stored.

<?php phpinfo();?>
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, you have to. link link
In this instance, the redirect is commented out, however it was firing fine. The problem is, the else shouldn't be firing.
Comments are often wrong. Check rfc and header Location syntax, RFC 2616 chapter 14.30. Mind also the uppercase 'L'.
@Splatter Are you exiting your script (die or exit) in your login script? Otherwise code might be executed that is below the redirect, causing your session to get messed up.
Again, the header is NOT the issue. When it's uncommented, it triggers fine. The problem is that that whole section of code should not be firing as $_SESSION['username'] should be set.
|
1

I would comment this but the under 50 rep limit means I can't for some reason. Try

$row = mysql_fetch_assoc($search);
$user=$row['forename'].' '.$row['surname'];
echo 'User: '.$user.'<br />';

$_SESSION['username']=$user;
echo 'Session: '.$_SESSION['username'];

//header( 'Location: page1.php' ) ;

and see if anything is actually being stored in the varibles.

EDIT: Try this

if($match > 0){
   $row = mysql_fetch_assoc($search);
   $user=$row['forename'].' '.$row['surname'];
   $_SESSION['username']=$user;
   $_SESSION['logintrue'] = true;
   //$msg = 'Login Complete! Thanks, '.$user.'!';
   header( 'Location: page1.php' ) ;
   die;
}

session.php

<?php
session_start();

if(!$_SESSION['logintrue'])
{
header( 'Location: login.php' ) ;
}

$SessionUsername = $_SESSION['username'];
?>

page1.php

<?php

require_once 'session.php';
require_once 'db.php';

echo 'I work now maybe?<br />';
echo $SessionUsername;
?>

Also includes aren't functions so write them like include 'db.php'; I've made that mistake aswell.

7 Comments

I put back in the commented line, modified to $msg = 'Login Complete! Thanks, '.$_SESSION['username'].'!'; and it returns the correct information. It seems it isn't persisting to the new page.
Try if(!empty($_SESSION['username'])){ echo 'Success, '.$_SESSION['username'].'!'; }else{ echo 'No dice!'; //header( 'Location: login.php' ) ; }
A good suggestion, but it return the same result. Doing an echo of $_SESSION['username'] returns nothing.
Well for last attempt try what I edited in the answer, its how I do my logins and it works for me.
I like the session control idea, looks great. Sadly, I replaces the header with an echo, which went off. So it looks like no session data is being held. Guess I need to figure out why.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.