Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help Logout destroy_session not working for me.

    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
    ================
    PHP Code:
    <?
    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 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 Code:
    <?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 Code:
    <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.
    Last edited by firepages; 02-19-2012 at 07:54 AM. Reason: removed user&pass

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,082
    Thanks
    11
    Thanked 99 Times in 97 Posts
    try
    PHP Code:
    unset($_SESSION); 
    which might give you better results, you can also explicitly delete the session cookie , see the example at http://php.net/manual/en/function.session-destroy.php though I have never found that necessary
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    try
    PHP Code:
    unset($_SESSION); 
    which might give you better results, you can also explicitly delete the session cookie , see the example at http://php.net/manual/en/function.session-destroy.php though I have never found that necessary

    Thank you sir....

  • #4
    Regular Coder
    Join Date
    Apr 2009
    Location
    Earth - sometimes sky :-)
    Posts
    121
    Thanks
    51
    Thanked 2 Times in 2 Posts
    Try this:

    PHP Code:
             // this should be in your login function or form
            
    session_start();
            
    $_SESSION['username'] = $username
    if you logged in then you started a session witch is recognized as the logged in user username. In my case it is the $username and I store it in the $_SESSION['username'].

    When you wish to log out and destroy the session then you have to do this:

    PHP Code:
            // this should be in your logout function or form
            
    session_start();
            unset (
    $_SESSION['username']);
            
    session_destroy(); 
    This should unset the stored session name variable and destroy the session .

  • #5
    New Coder
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by joda View Post
    Try this:

    PHP Code:
             // this should be in your login function or form
            
    session_start();
            
    $_SESSION['username'] = $username
    if you logged in then you started a session witch is recognized as the logged in user username. In my case it is the $username and I store it in the $_SESSION['username'].

    When you wish to log out and destroy the session then you have to do this:

    PHP Code:
            // this should be in your logout function or form
            
    session_start();
            unset (
    $_SESSION['username']);
            
    session_destroy(); 
    This should unset the stored session name variable and destroy the session .
    Many thanks sir..

  • #6
    Regular Coder
    Join Date
    Apr 2009
    Location
    Earth - sometimes sky :-)
    Posts
    121
    Thanks
    51
    Thanked 2 Times in 2 Posts
    Well done, if you can please click on "Thank User for this post" I'll appreciate it... :-)

    Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •