Imran Hasan Imran Hasan - 3 months ago 7
PHP Question

PHP: Session not working correctly in live server?

My sessions are working correctly in my localhost. But when I use it in live server it needs to be logged out first for login. For example when I have to login I will first logout then login otherwise it shows blank page. I don't know why this is happening.
This is my Login page

<?php
session_start();
if(isset($_SESSION['user_id']) && $_SESSION['login']=="true") {
header("location:userOwnProfile.php");
exit();
}
else
{

?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="register.css">
<script src="jquery-1.12.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<nav class="navbar nav-top navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="link one"><a href="index.php">Home</a></li>
<li class="link">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Blog</a>
</li>
<li class="link"><a href="#">FAQ</a></li>
<li class="link"><a href="#">Terms & Cond</a></li>
<li class="link"><a href="#">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="one"><a href="#">Facebook Login</a></li>
<li class="one"><a href="#">Login</a></li>
</ul>
</div>
</div>
</nav>


<div class="col-lg-4 col-lg-offset-4 col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 si">

<h3 class="center"><u>MEMBERS LOG IN</u></h3>
<BR>

<form action="Login.php" method="post">


<div class="row">
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-9">
<input type="email" name="user_email" class="form-control" id="email" placeholder="Enter email" required>
</div>
</div>

</div>
<div class="row">
<div class="form-group">
<label class="control-label col-sm-3" for="pwd">Password:</label>
<div class="col-sm-9">
<input type="password" name="user_password" class="form-control" id="pwd" placeholder="Enter password">
</div>
</div>
</div>

<div class="row">
<div class="col-lg-3 col-sm-3 col-md-3 ">

</div>
<div class="col-lg-9 col-sm-9 col-md-9 ">
<a href="#"><h4>Forgot Password</h4></a>
</div>
</div>

<div class="row">
<div class="col-lg-3 col-sm-3 col-md-3 ">

</div>
<div class="col-lg-9 col-sm-9 col-md-9 ">
<label><input type="checkbox"> Remember me</label>
</div>
</div>

<div class="row">
<div class="col-lg-3 col-sm-3 col-md-3 ">

</div>
<div class="col-lg-9 col-sm-9 col-md-9 ">
<!--<input class="btn btn-lg btn-danger">-->
<input type="submit" class="btn btn-lg btn-danger" name="submit" value="Login">
</div>
</div>





</form>
</div>
<?php
include("db.php");
/*if(isset($_POST['Go'])) { SIGNUP
$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];
$user_email = $_POST['user_email'];
echo $user_name . "<br>";
echo $user_email . "<br>";
echo $user_password . "<br>";
$sql = "INSERT INTO user(user_name,user_email,user_password) VALUE ('$user_name','$user_email','$user_password')";
if(mysqli_query($conn,$sql))
{
echo "stored";
header("location:http://localhost/window.php");
}
else
{
die(mysqli_error($sql));
}
}*/


if(isset($_POST['submit']))
{
$user_email = $_POST['user_email'];//real_escape_string
//echo $user_email;

$user_password = $_POST['user_password'];
$login_query="SELECT * FROM user WHERE user_email='$user_email' AND user_password='$user_password'";
$run=mysqli_query($conn,$login_query) or die($conn);
if(mysqli_num_rows($run)>0)

{
$res = mysqli_query($conn, "SELECT * FROM user WHERE user_email='$user_email'");

while ($record = mysqli_fetch_array($res)) {

$_SESSION['Name'] = $record['user_name'];
//$_SESSION['user_lname'] = $record['user_lname'];
//$_SESSION['user_status'] = $record['user_status'];
$_SESSION['user_email'] = $record['user_email'];
$_SESSION['user_id'] = $record['user_id'];
$_SESSION['login'] = "true";

}



//echo $_SESSION['user_fname'] . $_SESSION['user_lname'];
//echo "NAME: " . $_SESSION['Name'];

echo '<script>window.location="userOwnProfile.php"</script>';

}
else
echo "<p style='color: red; margin-top: -28px;'>User name or password incorrect</p>";
}




?>


<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="jumbotron bottom">

<div class="row bott">
<div class="col-lg-3 col-sm-3 col-xs-3 low">
<h3>Blog Feeds</h3>

</div>

<div class="col-lg-3 col-sm-3 col-xs-3 low">
<h3>Testimony</h3>

<h5>

</h5>

</div>

<div class="col-lg-3 col-sm-3 col-xs-3 low">
<h3>Facebook Like Box</h3>
</div>
</div>

</div>
</div>






</body>
</html></title>
</head>
<body>

</body>
</html>
<?php } ?>


This is the page which I will be redirected when I login

<?php
session_start();
if(!isset($_SESSION['login'])) {
header("location:Login.php");
}
else {
$user_id = $_SESSION['user_id'];
$user_email = $_SESSION['user_email'];
include("db.php");

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User's Own Profile</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="font-awesome-4.5.0/css/font-awesome.css">
<link rel="stylesheet" href="css/custom.css">
<link rel="stylesheet" href="main.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/newCustom.js"></script>


</head>
<body>

<div class="container">
<?php
include 'customNav.php'
?>

<!--Navbar-->


<div class="row topsecond">
<div class="col-lg-12">
<h3 class="heading">Ads</h3>
</div>
</div>

<!--Ads-->


<!-- <div class="row">
<div class="col-lg-8">
<div class="col-lg-3 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">My Profile</button>
</div>
<div class="col-lg-3 col-sm-5">
<a type="button" href="Search.php" class="btn btn-lg btn-primary">Find People</a>
</div>
<div class="col-lg-3 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">Message List</button>
</div>
</div>

<div class="col-lg-4">
<div class="col-lg-6 col-sm-5">
<a href="#">
<button type="button" class="btn btn-lg btn-primary">New Likes</button>
</a></div>
</a>

<div class="col-lg-6 col-sm-5">
<form>
<button type="button" class="g btn btn-lg btn-primary" ><span id="message"> <span class="glyphicon glyphicon-inbox"></span> </span>
</button>
<form>

</div>


</div>


</div>

-->




<!--Links-->


<div class="col-lg-8 left">

<div class="row">
<div class="row ">
<?php
$photo = mysqli_query($conn, "SELECT * FROM user WHERE user_id='$user_id'");

while ($recordPhoto = mysqli_fetch_array($photo)) {

if ($recordPhoto == FALSE) {
die(mysqli_error("error message for the user"));

}
?>
<div class="col-lg-4 col-sm-4 col-xs-4 img">

<img class="center-block img-rounded" src="<?php echo "../shadi/images/" . $recordPhoto['user_photo1'] ?>"
alt=""
width="80%" height="220px">
</div>

<div class="col-lg-4 col-sm-4 col-xs-4 img">

<img class="center-block img-rounded" src="../shadi/images/team-hire.jpg" alt="" width="80%" height="220px">
</div>

<div class="col-lg-4 col-sm-4 col-xs-4 img">
<img class="center-block img-rounded" src="../shadi/images/team-hire.jpg" alt="" width="80%" height="220px">
<?php
}
?>
</div>
</div>


<div class="row upload">
<!--<div class="col-lg-12 col-sm-5 col-xs-12">
<div class="col-lg-12 col-sm-5 col-xs-12">
<div class="form">
<h4 for="exampleInputFile">File input</h4>
<input type="file" id="exampleInputFile">
</div>
</div>
</div>-->
<div class="col-lg-4 col-sm-5 col-xs-12">
<a href="userOwnProfileSetting.php">
<div class="btn btn-lg btn-info edit">
Edit Profile
</div>
</div>
</a>

</div>
</div>
<?php
$res = mysqli_query($conn, "SELECT * FROM user WHERE user_id=$user_id");

while ($record = mysqli_fetch_array($res)) {

if ($record == FALSE) {
die(mysqli_error("error message for the user"));

}
?>
<div class="col-lg-12 col-sm-12">
<div class="row">
<h3 class="heading col-sm-4"><u>Profile Settings</u></h3>

<div class="col-sm-8">
<h3><u><?php echo $record['user_name']; ?></u></h3>
</div>
</div>
<div class="">
<div class="row">
<h4 class="col-sm-4">Gender :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_gender']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Date of Birth :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_dob']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Age :</h4>

<div class="col-sm-6 col-sm-6 col-xs-12">
<h5>31 years</h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Height :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_height']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Weight :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_weight']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4">Race :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_race']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4">Religion :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_religion']; ?></h5>
</div>
</div>
</div>
<br>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Country :</h4>

<div class="col-sm-6">
<h5> <?php echo $record['user_country']; ?> </h5>
</div>
</div>
</div>


<div class="">
<div class="row">
<h4 class=" col-sm-4">State Origin :</h4>

<div class="col-sm-6">
<h5> <?php echo $record['user_stateOfOrigin']; ?>
</h5></div>
</div>
</div>


<div class="">
<div class="row">
<h4 class=" col-sm-4">State Living :</h4>

<div class="col-sm-6">
<h5> <?php echo $record['user_stateLiving']; ?></h5>
</div>
</div>
</div>


<div class="">
<div class="row">
<h4 class=" col-sm-4">Marital Stauts :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_maritalStatus']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Career :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_career']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class=" col-sm-4">Educational Level :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_educationLevel']; ?></h5>
</div>
</div>
</div>
<br>

<div class="">
<div class="row">
<h4 class="col-sm-4">Relations for</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_relationshipfor']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class="col-sm-4">Plygamy :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_polygamy']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class="col-sm-4">Interest :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_interest']; ?></h5>
</div>
</div>
</div>

<div class="">
<div class="row">
<h4 class="col-sm-4">Smoking :</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_smokingVape']; ?></h5>
</div>
</div>
</div>

<div class="row">
<h4 class=" col-sm-4">Self Summary : <br>
</h4>

<div class="col-sm-6">
<h5><?php echo $record['user_selfSummary']; ?></h5>
</div>
</div>
<br>


<div class="row">
<div class="col-sm-4">

</div>
<!--<div class="col-sm-8">
<button type="button" class="btn btn-lg btn-warning">Submit</button>
</div>-->
</div>
</div>

</div>

<!--form done-->


<div class="col-lg-4">
<div class="row right">
<h2>Ads</h2>
</div>
<div class="row yes">
<h2 class="status"><u>Status</u></h2>
<h4 class="gly"><span class="glyphicon glyphicon-heart"></span><?php echo $record['user_Likes'] . " Likes"; ?></h4>


<h4 class="gly"><span class="glyphicon glyphicon-user"></span> Normal Member</h4>
<h4 class="gly"><span class="glyphicon glyphicon-minus-sign"></span> Offline</h4>
</div>

<div class="row yes">
<h3 class="status"><u>I'm looking for</u></h3>

<p class="nigga"> <?php echo $record['user_lookingFor']; ?> :D</p>
</div>

<div class="row yes">
<h3 class="status"><u>Facebook Like Box</u></h3>
</div>
</div>


<?php
}
?>





</div>

</body>
</html>
<?php
}
?>


Code for logout

<?php
session_start();
unset($_SESSION['Name']);
unset($_SESSION['user_email']);
unset($_SESSION['user_id']);
unset($_SESSION['login']);
header("location:index.php");

session_destroy();
exit();
?>


I want to know that why this is happening in live server. Why I need to logout first to get myself logged in.

Answer

Your userOwnProfile.php script sending output before sending header. There are some spaces before <?php tag. Remove those spaces. Hope this will help