I have using session variable for log in form validation. When the user gives correct name and password then the session variable will set and the page will redirect. The page checks the session variable and load related contents.
My code works fine in local server, but in online server, the session variable was not set.
admin.php
<?php
session_start();
$admin = 0;
if(isset($_SESSION['admin'])){
$admin = $_SESSION['admin'];
}
if($admin == 0){
?>
<form action="" class="login">
<label>User Name :</label>
<input type="text" class="uname"/>
<label>Password :</label>
<input type="password" class="pwd"/>
<input type="submit" class="lSubmit" value="SUBMIT"/>
<p class="alert lAlert">test alert</p>
</form>
<?php }elseif($admin == 1){ ?>
<h1>Welcome Site Admin..!!</h1>
<?php } ?>
jQuery
$('.lSubmit').click(function(){
var name = $('.uname').val();
var pwd = $('.pwd').val();
$.post("validation/login.php",{name:name,pwd:pwd}).success(function(data){
var obj = jQuery.parseJSON(data);
if(obj.success == 1){
$('.alert').css('color','#067800');
window.location = "/admin.php";
}else{
$('.alert').css('color','#CC0000');
}
$('.lAlert').text(obj.msg);
$('.lAlert').fadeIn('slow');
});
return false;
});
validation/login.php
<?php
session_start();
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$err['success'] = 0;
$err['msg'] = '';
if($name == ''){
$err['msg'] = 'Name required';
}else if($pwd == ''){
$err['msg'] = 'Password required';
}else if($name != 'admin'){
$err['msg'] = 'Wrong username';
}else if($pwd != 'admin'){
$err['msg'] = 'Wrong password';
}else{
$err['msg'] = 'Success';
$err['success'] = 1;
$_SESSION['admin'] = 1;
}
echo json_encode($err);
?>
When user gives name and password as admin, it was successfully loaded the welcome text in local server. But in online server the form only loaded again. The $_SESSION['admin']
was not set in online server. Can anybody help me?