I am creating a site that requires a login before you can do anything, the flow for this is as follows:
- User inputs username and password then clicks "login"
- Username & password is sent to a JavaScript page for jQuery ajax to another php page to be dealt with
- The PHP script deals with the request as appropriate and sets a session variable (`user_logged_in`) to either "1" or "0"
- Depending on what is returned, the page relocates to the index page or shows a message
My problem is that I use a config file to determine whether or not the user is logged in using this session variable, but for some reason (although the variable is set to 1), the page still says that it is not, the code is as follows:
Main login page:
<?php
require_once 'login_config.php';
$alert = false;
if (isset($_GET['alert']) && $_GET['alert'] == "1")
{
$alert=true;
}
//$_SESSION['user_logged_in'] = false;
//if ($_SESSION['user_logged_in'] == true)
//{
// header("Location: /");
//}
?>
<!DOCTYPE html>
<html>
<head>
<title>GWSAM » Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="styles/login_style.css" />
<script src="scripts/login/jquery.login.ajax.js"></script>
</head>
<body class="container-fluid">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<h1>GWSAM » Login</h1>
<?php if ($alert) { ?><p style="text-align: center;" class="alert alert-danger">You must login to access this.</p><?php } ?>
<table class="table table-condensed table-bordered table-striped">
<tr>
<td>
<label for="username">Username</label>
</td>
<td>
<input type="text" name="username" placeholder="Username" class="form-control" id="username" />
</td>
</tr>
<tr>
<td>
<label for="password">Password</label>
</td>
<td>
<input type="password" name="password" placeholder="Password" class="form-control" id="password" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" id="http-host" value="<?=$_SERVER['HTTP_HOST']; ?>" />
<button id="login" class="form-control btn btn-primary">Login</button>
</td>
</tr>
</table>
<div class="row">
<div class="col-sm-12"id="loading"></div>
</div>
</div>
<div class="col-sm-3"></div>
</div>
</body>
</html>
<?=var_dump($_SESSION);?>
JavaScript jQuery ajax page:
$( document ).ready( function ()
{
$( "#login" ).on( "click", function ()
{
$.ajax( {
url: "/scripts/login/files/login.php?username=" + $( "#username" ).val() + "&password=" + $( "#password" ).val(),
type: "GET",
success: function ( R )
{
if ( R == "true" )
{
document.write( '<meta http-equiv="refresh" content="0;URL=\''+$("#http-host").val()+'\'" />' );
window.location.href = $( "#http-host" ).val();
return false;
//$( "#loading" ).html( '<meta http-equiv="refresh" content="0; URL=index.php" />' );
//window.location.href = "index.php";
//return;
}
$( "#loading" ).html( R );
},
beforeSend: function ()
{
$( "#loading" ).html( '<p class="alert alert-info" style="text-align: center;">Logging in, please wait.<br /><img src="http://www.iggadget.com/assets/images/loader-1.gif" alt="spinner.gif" height="30" width="30" /></p>' );
}
} )
} );
} );
Ajax page:
<?php
require_once '../../../login_config.php';
function Login($username, $password)
{
global $link;
while ($result = mysqli_fetch_assoc($link->query("SELECT `username`, `password`, `active` FROM `users` WHERE `username`='{$username}';")))
{
if ($result['active'] == "NO")
{
print '<p class="alert alert-danger" style="text-align: center;">User\'s account has been deactived, please contact your network admin</p>';
$_SESSION['user_logged_in'] = "0";
return false;
}
if ($password == $result['password'])
{
$_SESSION['user_logged_in'] = "1";
return true;
}
}
$_SESSION['user_logged_in'] = "0";
return false;
}
print Login($_REQUEST['username'], $_REQUEST['password']) ? 'true' : '<p class="alert alert-warning" style="text-align: center;">Cannot login, please try again.</p>';
?>
Config page for the index.php file:
<?php
require_once 'functions/functions.php';
require_once 'functions/database.php';
if (!isset($_SESSION))
session_start();
$logged_in = isset($_SESSION['user_logged_in']) ? ($_SESSION['user_logged_in'] == "1" ? true : false ) : false;
if (!$logged_in)
{
header("Location: login.php");
}
else
{
//Null action
}
?>
The index page is shown after all is done, but it keeps sending it back to the login page...
I hope this is enough information to go on...
session_start()starts the session...?session_start()to the top of every page. Dont test first and then start it, just start it