tags:

views:

266

answers:

1

I am creating a user login system similar to a client intranat. And when I try to access the main page I get a redirect loop in FF.

I am checking to see if a user is logged in with this:

if(($_SERVER['PHP_SELF'] != '/webmaster/index.php') && ($_SESSION['loggedin'] != '1234')){
  header("Location: ".$domain."index.php?l=no");
  exit();
}

Below is my process-login.php -> which is the file that handles client login:

<?php
ob_start();
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL | E_NOTICE);
include ("config.inc.php");
include ("jsonEncode.php");

// username and password sent from form
$username = '';
$password = '';
$username = mysql_real_escape_string($_GET['username']);
$password = mysql_real_escape_string($_GET['password']);

$sql    = "SELECT * FROM clients WHERE username='$username' AND password='$password' LIMIT 1";
//echo $sql;
$result = mysql_query($sql);

$data   = mysql_fetch_array($result);
$count  = mysql_num_rows($result);

if($count==1){
    $_SESSION['username']  = $username;
    $_SESSION['password']  = $password;
    $_SESSION['client_id'] = $data['c_id'];
    $_SESSION['loggedin']  = "1234";

    /*
    echo $_SESSION['client_id'];
    echo $_SESSION['password'];
    echo $_SESSION['username'];
    */
    echo $_SESSION['loggedin'];
    // valid
    $var = array('valid' => 1, 'username' => $username, 'password' => $password);
    print php_json_encode($var);

}else{
    // invalid
    $var = array('valid' => 0, 'username' => $username, 'password' => $password);
    print php_json_encode($var);
}
?>

The main index.php page has two forms one for clients and one for webmastsers, and if you are a client you are redirected too: clients/ and if you're a webmaster you're redirected too: webmaster/

I have checked my login scripts and it is returning the right information and logging it in, but it keeps looping.

Any ideas on what I can check or any other details I can provide you.

The form is submitted via ajax then returns JSON with a value of 1 being valid or 0 invalid to see if the user can continue

Thanks,

Ryan

+1  A: 

If the form is submitted through AJAX are you sure that the session cookie is set accordingly? I know that Firefox will send cookie information together with asynchronous requests but are you confident that it will work the other way around?

if (($_SERVER['PHP_SELF'] != '/webmaster/index.php') && 
    ($_SESSION['loggedin'] != '1234')) { // I don't like this!
    header("Location: ".$domain."index.php?l=no");
    exit();
}

The $_SESSION['loggedin'] value would be != '1234' most of the time and this would be the case initially, you should check whether the value is undefined as well and act accordingly. What guarantees do you have right now that if the user requests index.php that $_SESSION['loggedin'] is not != '1234' if this is a new session? Otherwise you'll have a redirection loop which can be caused if the AJAX response doesn't set a session cookie accordingly, assuming you use session cookies to track user session?

John Leidegren
Hey,I try it out by actually passing the page parameters in the URL and I get:{"valid":1,"username":"demo","password":"demo"} Which returns the correct part of the query, which means the account info is correct. Then I set the SESSION here, and redirect using javascript.
Coughlin
I print the SESSION on the page and it outputs the 1234 which means it is set and I check for that. Any ideas?
Coughlin
Are you sure that there ain't something wrong with that entire setup, what scripts are involved what JavaScript is being called and how does this all work together. Basically you have 3 states that you need to think about. Initial state, successful and unsuccessful login.
John Leidegren