help me fix this session code.
Posted: Wed May 30, 2007 6:08 am
Hi
I use the code below. but the first time i load the page, it says:
Notice: Undefined index: username in g:\programs(2)\easyphp1-8\www\ha\accesscontrol.php on line 5
Notice: Undefined index: pass in g:\programs(2)\easyphp1-8\www\ha\accesscontrol.php on line 6
Login Required
You must log in to access this area of the site. If you are not a registered user, click here to sign up for instant access!
then bring the login form
after i enter the username ans pass, it brings the form again and asks for username and pass"
please help me setup my session code. thanks
accesscontrol.php:
db.php:
is not required to be mentioned here but to complete, common.php:
I use the code below. but the first time i load the page, it says:
Notice: Undefined index: username in g:\programs(2)\easyphp1-8\www\ha\accesscontrol.php on line 5
Notice: Undefined index: pass in g:\programs(2)\easyphp1-8\www\ha\accesscontrol.php on line 6
Login Required
You must log in to access this area of the site. If you are not a registered user, click here to sign up for instant access!
then bring the login form
after i enter the username ans pass, it brings the form again and asks for username and pass"
please help me setup my session code. thanks
accesscontrol.php:
Code: Select all
<?php
session_start();
include_once 'db.php';
include_once 'common.php';
$uid = isset($_POST['username']) ? $_POST['username'] : $_SESSION['username'];
$pwd = isset($_POST['pass']) ? $_POST['pass'] : $_SESSION['pass'];
if(!isset($username)) {
?>
<!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>
<title> Please Log In for Access </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1> Login Required </h1>
<p>You must log in to access this area of the site. If you are
not a registered user, <a href="signup.php">click here</a>
to sign up for instant access!</p>
<p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">
User ID: <input type="text" name="username" size="8" /><br />
Password: <input type="password" name="pass" SIZE="8" /><br />
<input type="submit" value="Log in" />
</form></p>
</body>
</html>
<?php
exit;
}
$_SESSION['username'] = $_POST['username'];
$_SESSION['pass'] = $_POST['pass'];
$username = $_POST['username'];
$pass = $_POST['username'];
dbConnect("articles");
$sql = "SELECT * FROM user WHERE username = '$username' AND pass ='$pass'";
$result = mysql_query($sql);
if (!$result) {
error('A database error occurred while checking your '.
'login details.\\nIf this error persists, please '.
'contact you@example.com.');
}
if (mysql_num_rows($result) == 0) {
unset($_SESSION['username']);
unset($_SESSION['pass']);
?>
<!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>
<title> Access Denied </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1> Access Denied </h1>
<p>Your user ID or password is incorrect, or you are not a
registered user on this site. To try logging in again, click
<a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To register for instant
access, click <a href="signup.php">here</a>.</p>
</body>
</html>
<?php
exit;
}
$username = mysql_result($result,0,'username');
?>Code: Select all
<?php // db.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
function dbConnect($db='') {
global $dbhost, $dbuser, $dbpass;
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
or die('cannot connect to database');
if ($db!='' and !@mysql_select_db(articles))
die('database is not available');
return $dbcnx;
}
?>Code: Select all
<?php // common.php
function error($msg) {
?>
<html>
<head>
<script language="JavaScript">
<!--
alert("<?=$msg?>");
history.back();
//-->
</script>
</head>
<body>
</body>
</html>
<?
exit;
}
?>