PHP Session working on localhost but not working on server

205

Thanks to be into this matter, I am having trouble to login in with session variable on server, while it's working perfect on localhost. Here is my code.

My landing page

<?php
require_once 'lock.php';
?>
My HTML code will be here....

lock.php

<?php
session_start();
require_once 'includes/connect.php';
if(empty($_SESSION['logged_in'])){
    header('Location: login.php?action=not_yet_logged_in');
}
$action = $_GET['action'];
if($action=='logout'){
    session_destroy();
    header('Location: login.php?action=logout');
}
?>

login.php

<?php
session_start();
require_once 'includes/connect.php';
if(isset($_SESSION['logged_in'])==true){
  header('Location: index.php?action=already_logged_in');
}
?>


<?php
if (isset($_GET['action'])) {
if($_GET['action']=='not_yet_logged_in'){
  echo "<div class='infoMesssage'>You cannot go to the index page because you are not yet logged in.</div>";
  }else if($_GET['action']=='logout'){
    echo "<div class='infoMesssage'>You have scuccessfully logout.</div>";
  }
}          

 if($_POST){
  $enUsername = $_POST['username'];
  $enPassword = $_POST['password'];

  if(empty($_POST['username'])){
    $username = 'mockadmin';
    $password = 'mockadmin';
  }else {
    $chLogin = mysqli_query($con, 'select * from admin where user_name="'.$enUsername.'" AND password="'.$enPassword.'"');
    $chUser = mysqli_fetch_object($chLogin);

    $userId = $chUser->id;
    $username = $chUser->user_name;
    $password = $chUser->password;
  }

  if($_POST['username']==$username && $_POST['password']==$password){
   $_SESSION['logged_in'] = true;
   header('Location: index.php');        
  } else{
   echo "<div class='failedMessage'>Access denied. :(</div>";
  }
}
?>
<form method="post" role="form" action="login.php">
  <div >
    <input type="text" name="username" placeholder="Username">
  </div>
  <div >
     <input type="password" name="password" placeholder="Password">
  </div>
  <button type="submit" name="btnlogin" >Login</button>
</form>

My database connect.php

<?php  
$con = mysqli_connect('localhost', 'liveuser', 'livepass', 'mocktest');
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

I will appreciate your help. Thanks Again.

People are also looking for solutions to the problem: php - AS3 dynamic data loading incorrectly

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.