Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:26:36+00:00 2026-06-17T23:26:36+00:00

i have written login and logout scripts in php.I am able to log in

  • 0

i have written login and logout scripts in php.I am able to log in but log out is not working.Someone please point the mistake.i am completely lost.Here’s the complete code.

1.the log in php code:

    <?php
session_start();
require_once('login/connectvars.php');
$msg = "";

if(!isset($_SESSION['username']) && isset($_COOKIE['username']))
{
    $_SESSION['username'] = $_COOKIE['username'];
}

if(isset($_POST['submit']))
{
    $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
    $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
    $password = mysqli_real_escape_string($dbc,trim($_POST['password']));

    if(!empty($username) && !empty($password))
    {
        $query = "SELECT username FROM users_dns WHERE username = '$username' AND password = SHA('$password')";
        $data = mysqli_query($dbc,$query) or die(mysqli_error($dbc));

        if(mysqli_num_rows($data) == 1)
        {
            $row = mysqli_fetch_array($data);
            $_SESSION['username'] = $row['username'];
            setcookie('username',$row['username'],time()+(60*6));
            $msg = 'success';
        }
        else
        {
            $msg = 'enter correct values..';
        }
    }
    else
    {
        $msg = 'Enter all fields..';
    }
}

?>

2.the code i used in HTML to check the session.

   <div id="login_form">
          <h2>
            <?php echo $msg; ?>
          </h2>
       <?php
       if(isset($_SESSION['username']))
        {
            echo 'nothing<br />';
            echo '<a href="login/logout.php">log out'.$_SESSION['username'].'</a><br />';
        }
        else
        {
       ?>
         <form id="login"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table>
                  <tr>
                     <th>Username:</th>
                     <th>Password:</th>
                  </tr>
                  <tr>
                     <td><input type="text" name="username" /></td>
                     <td><input type="password" name="password" /></td>
                   </tr>
                   <tr>
                      <td><input type="submit" name="submit" value="Log In" class="submit" /></td>
                      <td><a href="login/signup.php"><input type="button" class="submit" value="Sign Up" /></a></td>
                   </tr>
            </table>
         </form>
         <?php
         }
         ?>
    <!-- login form -->

   </div>

3.The logout script

    <?php
session_start();

if(isset($_SESSION['username']))
{
    $_SESSION = array();

    if(isset($_COOKIE[session_name()]))
    {
        setcookie(session_name(),'',time() - 3600);
    }
    session_destroy();
}

setcookie('username','',time() - 3600);

echo 'redirecting you...please wait..';
header("Refresh: 3;url=http://localhost/");
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:26:37+00:00Added an answer on June 17, 2026 at 11:26 pm

    For deleting all cookies try.

    <?php
    
    if (isset($_SERVER['HTTP_COOKIE']))
    {
        $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
        foreach($cookies as $cookie)
        {
            $mainCookies = explode('=', $cookie);
            $name = trim($mainCookies[0]);
            setcookie($name, '', time()-1000);
            setcookie($name, '', time()-1000, '/');
        }
    }

    And for session you need to learn more about ob_start(); and ob_flush();.
    Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written php script for user login but instead of displaying the result
<a href=PHP/LogOut.php id=LogOut>Log Out</a> the link a#LogOut calls a PHP script that ends all
I have written the code below for a login page, but doesn't seem to
I have written a simplte PHP login script which starts a session: //Lets start
I have written a login form, in this I have used, form and method
I have written a django page that requires only super users to login. So
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I have a login page written in classic asp. I want to set the
I have this written in PHP: <?php global $current_user; get_currentuserinfo(); ?> <div style=float: right;
I would to take in some login information for a script have written in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.