Jump to content

sessions help please


Recommended Posts

I am working on a login script.  Here is the login page's code:

 

<?php

ob_start();

session_start();

require_once('db.php');

include('functions.php');

if(isset($_POST['Login']))

{

if($_POST['username']!='' && $_POST['password']!='')

{

$query = mysql_query('SELECT ID, Username, Active FROM users WHERE Username = "'.mysql_real_escape_string($_POST['username']).'" AND Password = "'.mysql_real_escape_string(md5($_POST['password'])).'"');

if(mysql_num_rows($query) == 1)

{

$row = mysql_fetch_assoc($query);

if($row['Active'] == 1)

{

$_SESSION['user_id'] = $row['ID'];

$_SESSION['logged_in'] = TRUE;

header('Location: http://www.mysite.com/new_page.php');

die();

}

else{

$error = 'Your membership was not activated. Please open the email that we sent and click on the activation link.';

}

}

else

{     

$error = '<font color="#FF0000" size=4>Wrong username or password.</font>';     

}

}

else{

$error = 'Please enter both your username and password to access your account.';

}

}

ob_flush();

?>

 

 

 

 

Then if the username and password are correct, the user gets redirected via the header function to the new page.  Then on the new page, I have this script:

 

<?php

ob_start();

session_start();

if (!isset($_SESSION['user_id']))

{

echo "You must be logged in to view this page";

die();

}

else

{

echo "Hello, you're logged in!";

}

ob_flush();

?>

 

And for some reason, when I login using a valid username and password, it says "You must be logged in to view this page."

 

I have tried everything I can think of.  Please help!!

Link to comment
https://forums.phpfreaks.com/topic/113631-sessions-help-please/
Share on other sites

Don't do a redirect like this:

 

header('Location: http://www.mysite.com/new_page.php');

 

You are forcing it to use www.mysite.com and you probably started your session using just mysite.com.  Use a relative path instead.

 

header('Location: new_page.php');

 

Link to comment
https://forums.phpfreaks.com/topic/113631-sessions-help-please/#findComment-583943
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.