Pregunta

I want to create a logout.php so that it remembers the username from the login.php.

I want to use $_GET method so that it gets the session username from login and when we logout, the cookie session is logged out. And if there is no set cookie session then there will be an error saying no ones were logged in so log gout doesn't work.

The login.php does login's the user and set cookie which I checked from the browser settings.

login.php

<?php 

require"connection.php";
if (!isset($_POST['submit'])){
    $user = $_POST['username']; 
    $password = $_POST['password'];  

  if ($user){ 
        if ($password){ 

setcookie('username', '$user', time()+3600); 
setcookie('password', '$password', time()+3600);       

          //make sure login info correct
            $query = mysql_query("SELECT * FROM users WHERE username = '$user'"); 
                $numrows = mysql_num_rows($query); 

            if ($numrows == 1) {
                $row = mysql_fetch_assoc($query); 
                 $dbuser = $row['username']; 
                 $dbpass = $row['password']; 

        echo '<script type="text/javascript">alert("Welcome, '.$user.'. A cookie session  has been created.");</script>'; 
        }else{echo "Please enter valid username or password";}
        }else {echo "Your password didn't match. Please try again";}
        }else {echo "Your username didn't match. Please try again";}
        }else {echo "Please enter username and password";}
?> 
¿Fue útil?

Solución

try something like this..

if(isset($_COOKIE['username']) && !empty($_COOKIE['username'])) {
    // Bye $_COOKIE['username'], you are logged out.
    setcookie('username', '', time() - 3600);
} else {
    // no user logged in
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top