skip to Main Content

I have a login script in Php. If the credentials are correct then the session is started, session variables are set and then redirected to the profile page. In the profile page, I have a script that redirects the user back to login page if they have not logged in.

Now, whenever I enter the correct credentials of the user and click on login, it redirects me back to the login page. To solve it, I commented out the code which was responsible for the redirection back to the login page. As a result, I got access to the profile page but I could not access the session variables.
And sometimes this code runs perfectly while sometimes it shows the above-stated problem.

The login code is as shown in the picture :

session_start();
require_once 'includes/config.php'; 
   if(isset($_POST['login'])){ 
    $user = trim($_POST['username']); 
    $pass = trim($_POST['password']);
    $ch = $_POST['position'];
    $stmt = $db->prepare("SELECT C_Name, PAN_id, Password FROM master_registration WHERE PAN_id = ?");
    $stmt->bindParam(1,$user);
    $stmt->execute();
    $row = $stmt->fetch();
           $username = $row["PAN_id"];
           $Name = $row["C_Name"];
           $hash = $row["Password"];
           if(password_verify($pass, $hash)) {
                session_start();
                $_SESSION["loggedin"] = true;
                $_SESSION["username"] = $username;
                $_SESSION["Name"] = $Name;
                header("Location: main_folder/master/profile.php");

Login page code

The profile page code is as shown in the picture:

 session_start();
 require_once '../../includes/config.php';
 if(!isset($_SESSION['loggedin']) && $_SESSION['loggedin'] !== true){
     header("location:../../index.php");
     exit;
 }
 $user = $_SESSION['username'];

profile page code

The seems fine, but there is a problem in sessions, plus it works in localhost but when I hosted in CPanel the problem starts.

Please help anyone…

2

Answers


  1. Change your profile pic code with this code…
    Your logic is incorrect thatswhy you are redirected everytime

    if(!$_SESSION['loggedin']) {
    header("location: ../../index.php");
    exit() ;
    } 
    
    Login or Signup to reply.
  2. Sometimes the Cpanel need config on the PHP SESSION, php.ini
    First yo can check the CPanel session.save_path and enabel output_buffering .
    to used phpinfo()
    Your code its correct. but if try session_start(); to inculed the config.php file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search