Hi to all .....
Istly have to mention here..I'm kind of newbie in php so my attempt just now is to make a simple login authentication using session....
Ok I able to do a login then set the session only my problem is when i Try to access in restricted page it's seem not recognise me..then it'll redirect to the login.php...eventhough after successful login...really weird..?... ok below I paste the code I currently using...
in index.php
<?
$PAGEINDEX = true;
include_once ('other/auth_user.php');
?>
then in auth_user.php
<?
/********************************************************/
function db_connection2(){
$dbname='shopcart';
$link=mysql_connect('localhost','root','');
mysql_select_db($dbname) or die(mysql_error());
return $link;
}
function db_select2($query){
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_fetch_array($result);
return $rows;
mysql_free_result($result);
}
function db_update2($query){
mysql_query($query) or die(mysql_error());
}
// ----------------------------------------------------------------------------------------
// functions
// ----------------------------------------------------------------------------------------
function auth()
{
global $mysession, $userid, $password, $varlatenter;
if (isset($_GET['bye']))
{ // user requested logout
session_start();
session_unregister("mysession");
session_destroy();
return 0;
}
if(isset($_POST['sented']))
{ // arrive from login form
$login_ok = 0;
if (isset($_POST['userid']) and isset($_POST['password']))
{
$db = db_connection2();
$rows = db_select2("SELECT * FROM useraccount WHERE userid='".$_POST['userid']."' AND password='".$_POST['password']."' LIMIT 1");
if ($rows)
{
session_start();
// create the session array
$mysession = array ("userid" => $_POST['userid'], "password" => $_POST['password'], "ID" => session_id());
///////////////////////////////
$userid = $_POST["userid"];
$password = $_POST["password"];
$varlatenter = $rows['lastenter'];
session_register("mysession");
$varlatenter = $rows['lastenter'];
db_update2("UPDATE useraccount SET lastenter=NOW() WHERE userid='".$_POST['userid']."'"); //last update
$login_ok = 1;
return 1; // authentication succeeded
}
mysql_close($db);
}
if(!$login_ok)
{
return 0; // access denied
}
}else{ // arrive from session var
$login_ok = 0;
session_start();
foreach($GLOBALS["mysession"] as $elem)
{ // retrieve session array
$ses_tmp[] = $elem;
}
$userid = $ses_tmp[0];
$password = $ses_tmp[1];
$db = db_connection2();
$rows = db_select2("SELECT * FROM useraccount WHERE userid='$userid' AND password='".$password."' LIMIT 1");
if ($rows)
{
session_start();
// create the session array
$mysession = array ("userid" => $userid, "password" => $password, "ID" => session_id());
session_register("mysession");
//////////////////////////////////////////
$login_ok = 1;
return 1; // authentication succeeded
}
mysql_close($db);
if(!$login_ok)
{
return 0; // access denied
}
} //end else
}//end function
// --------------------------------------------------------------------------------------
// main
// --------------------------------------------------------------------------------------
//init vars;
$mysession = array ("userid"=>FALSE, "password"=>FALSE, "ID"=>FALSE, "usertype"=>FALSE);
if(!auth())
{ // authentication failed
$logsuccessed = 0;
if($PAGEINDEX != true){
include("login.php"); // display login form
}
}else{ // authentication was successful
$logsuccessed = 1;
}
?>
and lastly in my restricted page
main.php
<?
//I just include the auth_user.php page only but it keeps redirect me eventhough after success login
include_once ('other/auth_user.php');
if($logsuccessed != 1){
exit();
}
?>
hope someone will point me or at least show me where is my wrong exactly...I'm alaso attach my file for u guys review...
p/s: auth_user.php must be put in other directory