Jump to content

user login works on my localhost computer but not on my webhost


viperjts10

Recommended Posts

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).

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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()..

 

Link to comment
Share on other sites

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

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.