Jump to content

PHP Session not Working


tonit

Recommended Posts

Hello, I'm trying to test a simple login system without a db, just for testing purposes. I have created a php file that checks if the username and pass are valid and register a session.

 

<?php
session_start();
define("ADMINUSER", "user");
define("ADMINPASSWORD", "pass");
$user = $_POST['user'];
$pass = $_POST['pass'];
if (($user==ADMINUSER) && ($pass==ADMINPASSWORD))
{
    $_SESSION['logged_in'] = true;
    header("Location: main.php");
}
else
{
    header("Location: auth.php?flag=wrong");
}
?>

 

and include this in other files to check if the user is logged in:

 

<? session_start();
if ($_SESSION['logged_in'] != true)
{
    header("Location: auth.php?flag=not");
    exit;
}
?>

 

But this doesn't seem to work, as I closed the window, clear my cookies and shutdown my PC and when I try to open it again and it doesn't redirect me to the login page(auth.php).

 

What am I doing wrong ? Any help is appreciated.

Link to comment
Share on other sites

What does a 'view source' in your browser show for the page with the second piece of code in it?

 

Ah, I changed the "<?" to "<?php" and it's working right now. Thank you very much, if it's that what you meant ^^.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.