0

I am setting up an admin panel for a website, and everything was working fine on my local (MAMP) server. I uploaded the website to the server and the user authentication isn't working anymore. I am able to get a success from the server, but when I'm entering into a page, PHP can't find the required session variable, and thus redirects the user back to the sign in page.

I have tried on both PHP version 5 and 7. I have tried echoing the session variable upon verification. I have tried to simply store the variable on one page and reading it on another page in the same folder, and it didn't work as well.

page1.php

<?php
session_start();
$_SESSION["userid"] = 1;
?>
<a href="page2.php">To Page2</a>

page2.php

<?php
session_start();
if (!isset($_SESSION["userid"])) {
    header("Location: page1.php");
    die();
}

echo $_SESSION["userid"];

After I click the link in page1.php, page2.php redirects me to page1.php again without any error.

Jaster
  • 56
  • 3
  • I'd say server misconfiguration, as your script works fine for me on my local environment. also ` – Scuzzy Jun 17 '19 at 03:08
  • I know it might sound silly but try clearing your browser cookies & cache. You might want to confirm if the session cookie is been created in the browser at all. – Pablo Jun 17 '19 at 03:08
  • use phpinfo() method and check if you can see the session in PHP configuration, compare it with your configuration of local – Hamed Ghasempour Jun 17 '19 at 03:33
  • See [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php), there may be an error preventing your session from working but you cannot see it – Phil Jun 17 '19 at 03:45
  • Thanks guys for your help. Really appreciates it! @Scuzzy I've tried this, and it outputs an empty array. @Pablo I cleared the cookies and cache, but I'm still getting an empty array from `var_dump($_SESSION);`. @HamedGhasempour `phpinfo();` states that session is enabled. Local settings: [link](https://pasteboard.co/IjTWjri.png), server settings: [link](https://pasteboard.co/IjTWWha.png). @Phil I enabled all error to be shown, but there's no error. – Jaster Jun 17 '19 at 23:46

1 Answers1

0

Is your website using some load balancer?

By default, PHP creates the session on server memory. So, if your website is using a different server for each request, the $_SESSION set values are lost. In this case, a good solution could be store sessions out of the server, maybe in a Redis or Memcached.