Jump to content

[SOLVED] Simple Login Script error


Custer

Recommended Posts

Hehe, well, I'm back with a login script, and it's a rather simple one, but I keep getting a blank page, and I ran this code by one of my programming friends and he couldn't find anything wrong, so perhaps one of you guys can. :)

 

<?PHP
init_set('display_errors',1); error_reporting(E_ALL);
//session start and connect
session_start();
require ("configure.php");
mysql_connect ("$dbhost", "$dbuser", "$dbpass")or die("Could not connect: ".mysql_error());
mysql_select_db("$dbname") or die(mysql_error());

//grabs information     
$username = $_POST['username'];
$password = md5($_POST['password']);

$username = mysql_escape_string($username);
$password = mysql_escape_string($password);


//checks data
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
$array = mysql_fetch_assoc($query);

if ($array['username'] == $username) {
	$1= '1';
	} else {
echo "The username does not exist.";
	die();
	}


if ($array['password'] == $password) {
	$2= '1';
	} else {
echo "Your password is incorrect.";
die();
}

if ($array['userlevel'] == '1') {
	$level = 'user';
	} elseif ($array['userlevel'] == '9') {
                $level = 'admin';
                } else {
echo "Please try again.";
die();
}


$_SESSION['username'] = $username;
if($1 == '1' AND $2 == '1' AND $level == '1') {
echo "Thank you for logging in : ".$username;
header ('Location: /members.php');}
elseif($1 == '1' AND $2 == '1' AND $level == '9')
{header ('Location: /admin.php');}
else {
echo "Extreme Error.";
die(); }

?>

Link to comment
Share on other sites

Hehe, well, I'm back with a login script, and it's a rather simple one, but I keep getting a blank page, and I ran this code by one of my programming friends and he couldn't find anything wrong, so perhaps one of you guys can. :)

 

<?PHP
init_set('display_errors',1); error_reporting(E_ALL);
//session start and connect
session_start();
require ("configure.php");
mysql_connect ("$dbhost", "$dbuser", "$dbpass")or die("Could not connect: ".mysql_error());
mysql_select_db("$dbname") or die(mysql_error());

//grabs information     
$username = $_POST['username'];
$password = md5($_POST['password']);

$username = mysql_escape_string($username);
$username = mysql_escape_string($password);


//checks data
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
$array = mysql_fetch_assoc($query);

if ($array['username'] == $username) {
	$1= '1';
	} else {
echo "The username does not exist.";
	die();
	}


if ($array['password'] == $password) {
	$2= '1';
	} else {
echo "Your password is incorrect.";
die();
}

if ($array['userlevel'] == '1') {
	$level = 'user';
	} elseif ($array['userlevel'] == '9') {
                $level = 'admin';
                } else {
echo "Please try again.";
die();
}


$_SESSION['username'] = $username;
if($1 == '1' AND $2 == '1' AND $level == '1') {
echo "Thank you for logging in : ".$username;
header ('Location: /members.php');}
elseif($1 == '1' AND $2 == '1' AND $level == '9')
{header ('Location: /admin.php');}
else {
echo "Extreme Error.";
die(); }

?>

 

haven't disseminated everything but your init_set at the top of the code should be "ini_set". also add...

 

ini_set('display_startup_errors', 1);

 

to make sure you catch everything, that should tell you the errors

 

 

Link to comment
Share on other sites

Okay, just retried it and got Extreme Error, which is weird, because I wrote it so that should never ever happen, because the username and password checks would put out their errors first...

 

EDIT:

 

Found the problem! On the $level == '1' in the final if statements, it's not 1 or 9, but user or admin. ;)

Link to comment
Share on other sites

Sorry for the triple post...

 

Okay, just tried it and got this error:

 

Thank you for logging in : test

Warning: Cannot modify header information - headers already sent by (output started at /www/newsit.es/c/u/s/custopoly/htdocs/login.php:49) in /www/newsit.es/c/u/s/custopoly/htdocs/login.php on line 50

 

Notice: Undefined index: log in /www/newsit.es/c/u/s/custopoly/htdocs/login.php on line 57

 

So my headers aren't working...

Link to comment
Share on other sites

$_SESSION['username'] = $username;

if($1 == '1' AND $2 == '1' AND $level == '1') {

echo "Thank you for logging in : ".$username;

header ('Location: /members.php');}

 

Not too sure but, if they're going to be redirected to members.php, why do you echo "Thank you for logging in"?

Link to comment
Share on other sites

rye, that wont work, you cant send header() by itself after any output has been printed to the screen because headers need to be generated before said output

 

can't remember exactly what you need... but output buffering is the way to do it if you want to stick with header() after printing

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.