viperjts10 Posted November 5, 2010 Share Posted November 5, 2010 Is there a reason why on my local machine with php, my login system works, but then when I upload my files and test it on my web host, the login system doesn't work properly. I'm using the same php version on my local machine and on my webhost, so I don't understand why it wouldn't work the same. When I try logging in on my webhost, everything processes as normal when a correct user/pass combo is found in the database, however, my session just doesn't seem to be saved, and therefore I won't be logged in. It'll end up refreshing to the home page (as I have it setup), but it won't show me as logged in. Is there something special I need to do in order for the session to be stored correctly? (I realize I haven't pasted any code, but I'm not sure exactly how much code would be needed for me to show in order to resolve the issue). Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2010 Share Posted November 5, 2010 Best guess is that your code is dependent on a php.ini configuration setting that is allowing it to work on your development system but not on your live host. You could also have a session setting problem on the live host. I would recommend temporarily setting error_reporting to E_ALL (it should already be this value) and display_errors to ON so that all the php detected errors will be reported and displayed. Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1130801 Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 My first guess is the session settings in your php.ini local and remote are different.. Can you post your login script (with page) and one secure page? Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1130802 Share on other sites More sharing options...
revraz Posted November 6, 2010 Share Posted November 6, 2010 Check your session save path in your production server php.ini file and make sure it's valid. Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1130927 Share on other sites More sharing options...
viperjts10 Posted November 6, 2010 Author Share Posted November 6, 2010 Yea, here's my login page and the session class: <?php if(isset($_POST['submit'])) { $username = trim($_POST['username']); $password = trim($_POST['password']); if(empty($username) || empty($password)) $e_msg = "One or more fields was not filled out"; else { $found_user = User::authenticate($username, $password); if($found_user) { $session->login($found_user); if($session->is_logged_in()) redirect_to('process.php?action=login'); } else $e_msg = "The username or password is incorrect. Please try again."; } } ?> <?php // It is inadvisable to store database related items in sessions. class Session { private $logged_in; public $user_id; function __construct() { session_start(); // Not working when uploaded to host. Says headers already sent $this->check_login(); } public function is_logged_in() { return $this->logged_in; } public function login($user) { // Database should find the user based on username/password if($user) { $this->user_id = $_SESSION['user_id'] = $user->id; $this->logged_in = true; } } public function logout() { unset($_SESSION['user_id']); unset($this->user_id); $this->logged_in = false; } private function check_login() { if(isset($_SESSION['user_id'])) { $this->user_id = $_SESSION['user_id']; $this->logged_in = true; } else { unset($this->user_id); $this->logged_in = false; } } } $session = new Session(); ?> My authenticate function in the User class: public static function authenticate($user="", $pass="") { global $db; //$user = $db->escape_value($user); // Not working?? //$pass = $db->escape_value($pass); // Not working?? $result = self::find_by_sql("SELECT * FROM " .self::$table_name. " WHERE username='{$user}' AND password='{$pass}' LIMIT 1"); return !empty($result) ? array_shift($result) : false; } Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1131101 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2010 Share Posted November 6, 2010 I don't see anywhere in the posted code where you tried setting the error_reporting/display_errors settings to the suggested values to see if there were any problems php would point out. You would need to set them right after the first opening <?php tag in your main file. Have any session variables worked on your live server? Have you tried a simple script that just sets a session variable on one page and tests if it carries over to another page? Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1131105 Share on other sites More sharing options...
OldWest Posted November 6, 2010 Share Posted November 6, 2010 If this is the error you are facing as you noted: session_start(); // Not working when uploaded to host. Says headers already sent Then you should should also (in addition to the other recommendations in this thread) consider any white-space or any other html prior to the output of the session_start().. Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1131112 Share on other sites More sharing options...
viperjts10 Posted November 7, 2010 Author Share Posted November 7, 2010 If this is the error you are facing as you noted: session_start(); // Not working when uploaded to host. Says headers already sent Then you should should also (in addition to the other recommendations in this thread) consider any white-space or any other html prior to the output of the session_start().. thanks for all the help everyone. I didn't realize I needed session_start() at the top of every page. I added that to my included header file (before all the html) and that seemed to resolve the problem. Awesome Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1131301 Share on other sites More sharing options...
OldWest Posted November 7, 2010 Share Posted November 7, 2010 be sure to mark thread as solved! Quote Link to comment https://forums.phpfreaks.com/topic/217877-user-login-works-on-my-localhost-computer-but-not-on-my-webhost/#findComment-1131302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.