theamazingninja Posted July 7, 2008 Share Posted July 7, 2008 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 More sharing options...
revraz Posted July 7, 2008 Share Posted July 7, 2008 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 More sharing options...
theamazingninja Posted July 7, 2008 Author Share Posted July 7, 2008 yes that was it! thank you! According to the header information on php.net, you have to put an absolute path when using the header function. This is good to know though. Thanks again. Link to comment https://forums.phpfreaks.com/topic/113631-sessions-help-please/#findComment-583966 Share on other sites More sharing options...
revraz Posted July 8, 2008 Share Posted July 8, 2008 Mark as solved please Link to comment https://forums.phpfreaks.com/topic/113631-sessions-help-please/#findComment-584410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.