Jump to content

login problems


Recommended Posts

I am not sure why my script is acting weird. It passes the username to the URL but the else statement does not act right. I gotta start going to bed earlier lol.

 

<?php 

require "includes/sql.php";

$action = $_GET['action'];

session_start();
ob_start();

$_SESSION['username'] = stripslashes($_POST['username']);
$_SESSION['password'] = stripslashes($_POST['password']);

$username = $_SESSION['username'];
$password = $_SESSION['password'];

switch($action)
{
	case "login":
		$result = mysql_query("SELECT * FROM users where username = '$username' AND password = '$password'");
		$num_rows = mysql_num_rows($result);

		if($num_rows > 0)
		{
			header("location: index.php?username=" . $username . "");
		} else {
			header("location: index.php");
		}
	break;

	case "logout":
		session_destroy();
		header("location: index.php");			
	exit;
	break;	
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Section</title>

<link href="adminstyles.css" rel="stylesheet" type="text/css" />

</head>

<body>

<div class="header">

<?php if(!$username)
	{ 
?>
	<div class="headertext">Admin Section! Please login below</div>

	</div>

		<div class="login">
			<form action="index.php?action=login" method="post">
				<div class="logincredentials">Username: <input type="text" name="username" id="username" /></div>
				<div class="logincredentials">Password: <input type="password" name="password" id="password" /></div>
				<input type="submit" value="Login" />
			</form>
		</div>

<?php } else { ?>

	<div class="headertext">Admin Section! Welcome <?php $username; ?></div>

	</div>

<?php } ?>

</body>
</html>

Link to comment
Share on other sites

Ive always had issues with using if (!$username) or something....  I've started doing something like this...

 

if ($_SESSION['username'] == '') {

  // error need to login..

} else {

  // logged in...

}

 

Also I noticed you missed an echo in your else...  should be echo $username, not $username...

 

-- radar

 

Just an edit..

 

if you use LIKE BINARY such as this:

$sql = "SELECT * FROM users

WHERE email LIKE BINARY '$formfields[username]'

AND password LIKE BINARY '$formfields[password]' AND d_id = '1' AND status = '1'";

 

it'll make your usernames and passwords case sensative.

Link to comment
Share on other sites

try putting this:

 

<?php echo $_SESSION['username']; ?>

 

Also here is some code to put at the bottom of every script you're coding (index.php's or the like)..

 

<?php

 

echo "<pre>SESSION:";

print_r($_SESSION);

echo "<br>COOKIE:";

print_r($_COOKIE);

echo "<br>POST:";

print_r($_POST);

echo "</pre>";

 

?>

 

that'll give you a print out of everything you're getting sent to the page you are on..  helps a lot with error problem solving.

Link to comment
Share on other sites

You probably don't want to write

<?php } else { ?>

	<div class="headertext">Admin Section! Welcome <?php $username; ?></div>

	</div>

<?php } ?>

 

BUT

 

<?php } else { ?>

	<div class="headertext">Admin Section! Welcome <?php echo $username; ?></div>

	</div>

<?php } ?>

 

you forgot "echo".

Link to comment
Share on other sites

ok sorry about not getting back with yall until now,

 

Here is my updated code:

 

<?php 

require "includes/sql.php";

$action = $_GET['action'];

session_start();
ob_start();

$_SESSION['username'] = stripslashes($_POST['username']);
$_SESSION['password'] = stripslashes($_POST['password']);

$username = $_SESSION['username'];
$password = $_SESSION['password'];

switch($action)
{
	case "login":
		$result = mysql_query("SELECT * FROM users where username = '$username' AND password = '$password'") or die("ERROR: " . mysql_error());
		$num_rows = mysql_num_rows($result);

		if($num_rows > 0)
		{
			header("location: index.php?username=" . $username . "");
		} else {
			header("location: index.php");
		}
	break;

	case "logout":
		session_start();
		session_destroy();
		ob_end_flush();
		header("location: index.php");			
	exit;
	break;	
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Section</title>

<link href="adminstyles.css" rel="stylesheet" type="text/css" />

</head>

<body>

<div class="header">

<?php if($_SESSION['username'] == "")
	{ 
?>
	<div class="headertext">Admin Section! Please login below</div>

	</div>

		<div class="login">
			<form action="index.php?action=login" method="post">
				<div class="logincredentials">Username: <input type="text" name="username" id="username" /></div>
				<div class="logincredentials">Password: <input type="password" name="password" id="password" /></div>
				<input type="submit" value="Login" />
			</form>
		</div>

<?php } else { ?>

	<div class="headertext">Admin Section! Welcome <?php echo $_SESSION['username']; ?> <a href="index.php?action=logout">Logout?</a></div>

	</div>

<?php } ?>

</body>
</html>

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.