Php login logout

hi all :slight_smile: i am creating user login logout system in my first website using php sessions

user logs in by providing email id and password i set email_id session variable and user is logged in

now i want to display logout instead of login when user is logged in

??

thanks

You can check the session to see if email_id is set, and display the correct link:


if(isset($_SESSION['email_id'])) {
    echo '<a href="logout.php">Log Out</a>';
} else {
    echo '<a href="login.php">Log In</a>';
}

the same i am doing but it is not working from me . i don’t know where is the problem,

i am posting complete code please have a look to and please help me out


<?php

session_start();
if(isset($_SESSION['email_id'])){
		header('location: index.php');
		exit();
	}		

?>

<?php

if(isset($_POST["email_id"]) && isset($_POST["customer_pass"]))
{
	$email_id= $_POST["email_id"];
	$customer_pass= $_POST["customer_pass"];
	include("includes/connect_to_mysql.php");
	$sql=mysql_query("select customer_id from customer where email_id='$email_id' AND pass='$customer_pass' LIMIT 1")  or die(mysql_error());
	$existCount=mysql_num_rows($sql);
	if($existCount==1)
	{
		while($row=mysql_fetch_array($sql))
		{
			$id=$row["id"];
			}
 			$_SESSION["email_id"]= $email_id;
			 header("location:index.php");
			exit();
		}
		else{
				echo'the informations you provided is incorrect, try again <a href="customer_login.php"> click here </a>';
				exit();
			}
	
	}

?>


<!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>Admin LogIn </title>




<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  

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




</head>

<body>

<div id="mainwrap">
 
 <?php
 require_once('header.php');
 
 ?>
		
		
        
        <!-- frimary navigation menu -->
        <?php
		
		require_once('menu.php');
		
		?>
                    
       <!-- page go back -->
                    

        <?php
		
		require_once('redirect.php');
		
		
		?>
           

          
         <div id="adim_lonin_main" align="center">
         	<div id="admin_login_heading" align="center">
            	<h2 align="center"> Log In Here!</h2>
            </div>
            
            
              
             
             <form   method="post"  action="customer_login.php" name="admin_login_form" id="admin_login_form">
             
              <table width="200" style="margin-top:30px;">
  <tr>
    <td>Enter your Email ID:</td>
    </tr>
   <tr>
    <td><label>
      <input type="text" name="email_id" id="admin_name" class="admin_form_field" />
    </label></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td>Entger your password:</td>
  </tr>
     <td> 
           <input type="password" name="customer_pass" id="admin_pass"  class="admin_form_field"/>
     </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="login" id="login" value="LogIn" />
     </td>
  </tr>
              </table>
 
           </form> 
             
              
  </div>
        <!-- footer -->
        
        <?php
		
		require_once('footer.php');
		
		?>
                   
	</div>
</body>
</html>

here is the code in “header.php”


<html>
<body>
<div id="headerwrap">

			<div id="header">
            	<div id="logowrap">
				<div id="logo">
                <a href="index.php"> <img   src="images/logo.png"
                  border="0"/>  </a>
				</div>
                </div>
				<div id="loginform">
                
 <?php
                	 
	
if (isset($_POST["email_id"]) && isset($_POST["customer_pass"])) {

	$email_id = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["email_id"]); 
	$customer_pass = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["customer_pass"]);  
	include"includes/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT id FROM customer WHERE customer_id='$customer_id' AND pass='$customer_pass' LIMIT 1");
	
	$existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
	     while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
		 }
 		 $_SESSION["email_id"] ; 
 		 header("location: index.php");
         exit();
    	} 
}
	 				if(isset($_SESSION['email_id'])){
							echo '<ul> <li> <a href="customer_logout.php">LogOut </a> </li> </ul>'; 
						 }else{
							 
							echo '<ul> <li> <a href="customer_login.php">LogIn </a></li> </ul>';
							 }
  					 
	 ?>
  			</div>

		</div>
     </body>
     
     </html>

Try this script, it has the login logout function in a single file http://codecanyon.net/item/member-loginmanager-activity-stream/3316956 I found it pretty useful:

session_start();
if($_COOKIE['login']=='1'||$_SESSION['login']=='1'){
$_SESSION['login']=$_COOKIE['login'];
$_SESSION['uid']=$_COOKIE['uid'];
}else{
header("Location: login.php");
}
require_once("functions/functions.php");