cabnet
02-19-2012, 02:08 AM
Hi!
i have a problem with my log out script i can log in but log out not working for me here are my codes.
My main.php this page supposed to be my private page you need to login with your true username and pass, before you can view this page, but still
accessible even without login you can access it directly to the browser. I need help on how to make this codes work properly maybe I just put the code in the wrong place..
==================
main.php
================
<?
session_start();
if(!session_is_registered("username")){
header("location:index.php"); // <--is this correct?
}
?>
<?
session_start();
session_destroy(); <--is this correct here i put my log out?
?>
<!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=utf-8" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to the main page</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="container">
<div id="header"><img src="../img/banner1.gif"/></div>
<div id="contents">
<table>
<ul>
<li><a href="Logout.php">LogOut</a></li>
</ul>
</table>
<h1>under construction</h1>
<table>
</table>
</div><!--contents-->
</div>
</body>
</html>
==================================
my check log in code
<?php
$host="localhost"; // Host name
$username="*********"; // Mysql username
$password="********"; // Mysql password
$db_name="my_db"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$mysql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($mysql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:main.html");
}
else {
echo "Wrong Username or Password";
}
?>
=========================
my success_login.php
<?php
session_start($username);
if(!session_is_registered($username)){
header("location:index.html");
}
?>
<html>
<body>
<h2>Login Successful</h2>
</body>
</html>
======================
My logout_php
<php?
session_start();
session_destroy();
?>
I need to know the where to put the log-out code? that when the user want to log-out it will redirect to my index
Many thanks in advance I hope You can help I am just the newbie in php tnx.
i have a problem with my log out script i can log in but log out not working for me here are my codes.
My main.php this page supposed to be my private page you need to login with your true username and pass, before you can view this page, but still
accessible even without login you can access it directly to the browser. I need help on how to make this codes work properly maybe I just put the code in the wrong place..
==================
main.php
================
<?
session_start();
if(!session_is_registered("username")){
header("location:index.php"); // <--is this correct?
}
?>
<?
session_start();
session_destroy(); <--is this correct here i put my log out?
?>
<!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=utf-8" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to the main page</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="container">
<div id="header"><img src="../img/banner1.gif"/></div>
<div id="contents">
<table>
<ul>
<li><a href="Logout.php">LogOut</a></li>
</ul>
</table>
<h1>under construction</h1>
<table>
</table>
</div><!--contents-->
</div>
</body>
</html>
==================================
my check log in code
<?php
$host="localhost"; // Host name
$username="*********"; // Mysql username
$password="********"; // Mysql password
$db_name="my_db"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$mysql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($mysql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:main.html");
}
else {
echo "Wrong Username or Password";
}
?>
=========================
my success_login.php
<?php
session_start($username);
if(!session_is_registered($username)){
header("location:index.html");
}
?>
<html>
<body>
<h2>Login Successful</h2>
</body>
</html>
======================
My logout_php
<php?
session_start();
session_destroy();
?>
I need to know the where to put the log-out code? that when the user want to log-out it will redirect to my index
Many thanks in advance I hope You can help I am just the newbie in php tnx.