Below is my accesscontrol.php
PHP Code:
<?php // accesscontrol.php
error_reporting (E_ALL ^ E_NOTICE);
include_once "./common.php";
include_once "./db.php";
include_once "./functions.php";
session_start();
$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];
if(!isset($uid)) {
login_form();
exit;
}
$_SESSION['uid'] = $uid;
$_SESSION['pwd'] = $pwd;
dbConnect("sessions");
$sql = "SELECT * FROM user WHERE
userid = '$uid' AND password = '$pwd'";
$result = mysql_query($sql);
if (!$result) {
error('A database error occurred while checking your '.
'login details.\\nIf this error persists, please '.
'contact [email="you@example.com"]you@example.com[/email].');
}
if (mysql_num_rows($result) == 0) {
unset($_SESSION['uid']);
unset($_SESSION['pwd']);
login_form();
exit;
}
$username = mysql_result($result,0,'fullname');
$_SESSION["myname"]=$username;
function ckdisplayname(){
//global $username;
echo "<P>";
echo "<P>";
echo "<P>";
echo $_SESSION["myname"];
echo $username; // no display
}
?>
My functions.php:
PHP Code:
<?
// functions.php
function login_form() {
global $PHP_SELF, $HTTP_REFERER, $HTTP_HOST, $REQUEST_URI;
global $uid, $pwd, $validate, $logfailed;
$listing_title="Surfmark's login form";
$msg="";
$format="";
echo '<br>';
//tableform1a($listing_title);
if ($logfailed && $uid == '') {
$msg .= "- Please fill in the uid field!<br>";
$format = "BAD";
}
if ($logfailed && $pwd == '') {
$msg .= "- Please fill in the your password field!<br>";
$format = "BAD";
}
if ($logfailed && $uid && $pwd && have_validated($uid) && incorrect_log_code($uid, $pwd)){
$msg .= "- Login failed. Please make sure your have typed in your uid and password correctly.<br>";
$format = "BAD";
}
if ($logfailed && $uid && $pwd && !have_validated($uid)){
$msg .= "- Possibility 1: There is no such uid in our database.<br>";
$msg .= "- Possibility 2: Membership has not been validated.<br>";
$format = BAD;
}
//$furl="[url="http://".$_server['http_host'].$request_uri/"]http://".$_SERVER['HTTP_HOST'].$REQUEST_URI[/url];
$furl=$_SERVER['PHP_SELF'];
//$_SERVER['REQUEST_URI']
//$furl=$HTTP_REFERER;
//$furl=$PHP_SELF;
// <FORM METHOD="POST" ACTION="<? echo $furl
?>
<FORM METHOD="POST" ACTION="<? echo $furl?>">
<table border="0" cellpadding="3" width="100%">
<tr>
<td width="100%" align="right" height="19" colspan="3">
<p align="left"><b><font color="#000080">Please log in to access the page
you requested.</font></b></td>
</tr>
<? if ($msg) { ?>
<tr>
<td width="100%" align="right" height="19" valign="top" colspan="3">
<p align="left"><b><font color="#FF0000">Error :</font></b><br><?=$msg;?><br>
</td>
</tr>
<? } if ($format=="BAD" || !$logfailed) { # if format equal to BAD OR NO $logfailed ?>
<tr>
<td width="18%" align="right" height="20">User ID</td>
<td width="3%" align="center" height="20">:</td>
<td width="79%" height="20" align="left">
<input name="uid" size="20" value="<?=$uid;?>"></td>
</tr>
<tr>
<td width="18%" align="right" height="22">Password</td>
<td width="3%" align="center" height="22">:</td>
<td width="79%" height="22" align="left">
<input name="pwd" size="20" type="password" value="<?=$pwd;?>"></td>
</tr>
<tr>
<td width="18%" align="right" height="19">Action</td>
<td width="3%" align="center" height="19">:</td>
<td width="79%" height="19" align="left">
<INPUT TYPE="submit" VALUE="Login" NAME="Submit" align="left"></td>
</tr>
<tr>
<td width="100%" height="19" valign="top" colspan="3">
</td>
</tr>
</table>
</FORM>
<?
} // ends if ($format == BAD || !$logfailed) {
} // ends login_form()
function do_Logout() {
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
session_unset();
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
//session_start();
//unset($uid);
//unset($pwd);
// Unset all of the session variables.
//$_SESSION = array();
// Finally, destroy the session.
//session_destroy();
//unset($_SESSION['uid']);
//unset($_SESSION['pwd']);
}
?>
I use the above script to protect several scripts:
PHP Code:
<?php // protectedpage2.php
include 'accesscontrol.php';
switch($a) {
case "logout":do_logout();break;
default:
message();
break;
}
function message(){
?>
<html xmlns="[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]">
<head>
<title> Members-Only Page </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1
</head>
<body>
<p>Welcome, <?=$username?>! You have entered a members-only area
of the site. Don't you feel special?</p>
<p><a href="[url="http://localhost/accesscontrol/index.php?a=logout"]http://localhost/accesscontrol/index.php?a=logout[/url]">
[url="http://localhost/accesscontrol/index.php?a=logout</a></p"]http://localhost/accesscontrol/index.php?a=logout</a></p[/url]>
<p><a href="[url="http://localhost/accesscontrol/protectedpage3.php?a=logout"]http://localhost/accesscontrol/protectedpage3.php?a=logout[/url]">
[url="http://localhost/accesscontrol/protectedpage3.php?a=logout</a></p"]http://localhost/accesscontrol/protectedpage3.php?a=logout</a></p[/url]>
</body>
</html>
<? } ?>
PHP Code:
<?php
// protectedpage3.php
include 'accesscontrol.php';
switch($a) {
case "logout":do_logout();break;
default:
message();
break;
}
function message(){
?>
<html xmlns="[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]">
<head>
<title> Members-Only Page </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1
</head>
<body>
<p>Welcome, <?=$username?>! You have entered a members-only area
of the site. Don't you feel special?</p>
<p><a href="[url="http://localhost/accesscontrol/protectedpage2.php?a=logout"]http://localhost/accesscontrol/protectedpage2.php?a=logout[/url]">
[url="http://localhost/accesscontrol/protectedpage2.php?a=logout</a></p"]http://localhost/accesscontrol/protectedpage2.php?a=logout</a></p[/url]>
<p><a href="[url="http://localhost/accesscontrol/protectedpage3.php?a=logout"]http://localhost/accesscontrol/protectedpage3.php?a=logout[/url]">
[url="http://localhost/accesscontrol/protectedpage3.php?a=logout</a></p"]http://localhost/accesscontrol/protectedpage3.php?a=logout</a></p[/url]>
</body>
</html>
<? } ?>
Tried many of them but no working ...
.
Help needed. Thanks in advance.
Bookmarks