<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<!--[if lt IE 7 ]> <html class="no-js loading ie6 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 7 ]>    <html class="no-js loading ie7 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 8 ]>    <html class="no-js loading ie8 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js loading" dir="ltr" lang="en-US"> <!--<![endif]-->
<?php
        session_start();
        define( "USERNAME", "john" );
        define( "PASSWORD", "secret" );

        if ( isset( $_POST["login"] ) ) {
            login();
        } elseif ( isset( $_GET["action"] ) and $_GET["action"] == "logout" ) {
            logout();
        } elseif ( isset( $_SESSION["username"] ) ) {
        displayPage();
        } else {
        displayLoginForm();
        }

        function login() {
        if (isset( $_POST["username"] ) and isset( $_POST["password"] ) ) {
            if ($_POST["username"] == USERNAME and $_POST["password"] == PASSWORD ){
                $_SESSION["username"] = USERNAME;
                session_write_close();
                header ('Location: loginpage.php');
                } else {
                displayLoginForm ('Sorry, that username/password could not be found. Please try again.');
                }
            }
        }
        function logout()
        {
        unset( $_SESSION["username"] );
        session_write_close();
        header('Location: loginpage.php');
        }   
        function displayPage(){
            displayPageHeader();
    ?>
    <p> Welcome, <strong > <?php echo $_SESSION["username"] ?>
    </strong > ! You are currently logged in. </p >
    <p> <a href= ” loginpage.php action=logout”> Logout </a> </p>

    <?php
        }
        function displayLoginForm( $message="" )
        {
        displayPageHeader();
    ?>
<html>
    <head>
    <meta charset="UTF-8" />
    <title>Student Registration-NPCS</title>
    <meta name="description" content="Management Software National Pre Cadre High School" />
    <meta name="keywords" content="HTML5, Wordpress" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="shortcut icon" href="favicon.png" />
    <link rel="apple-touch-icon" href="img/touch-icon.png" />
    <link rel="image_src" href="img/touch-icon.png" />

    <link rel="stylesheet" href="css/style.css" />
    <script type='text/javascript' src='js/jquery.min.js'></script>
    <script type='text/javascript' src='js/jquery.plugins.min.js'></script>
    <script type='text/javascript' src='js/site.js'></script>

    <!--[if IE]>
    <script type="text/javascript" src="js/ie.js"></script>
    <script type="text/javascript" src="js/respond.min.js"></script>
    <![endif]-->  

    </head>

    <body>
    <div class="border-header"></div>
    <section id="header" class="container">
        <header id="logo" class="grid3 col">
            <hgroup>
                <h1><a class="animated shake" href="index.html" rel="home" title="Main Page!"><img src="img/logo-gray-yellow.png" class="scale" width="180" height="67" alt="CleanGold"/></a></h1>
            </hgroup>
        </header>
        <div class="sep sep-small"></div>
        <span id="switch">Menu <strong>&#8801;</strong></span>
    </section>
    <section id="content" class="container">
        <a class="button big full-width gray animated shake">Management Control System</a>
    </section>
<br>
    <br>
        <br>
            <section id="content" class="container" align="center">
        <div class="grid12 col" align="center" >
            <?php
                if ($message) echo '<p class="error">' .$message. '</p>';
             ?>
                   <form action=" loginpage.php " method="post" >
                       <div style='width: 30em;' >
                         <label for="username" > Username </label >
                         <input type="text" name="username" id="username" value="" />
                         <label for=”password" > Password </label >
                         <input type="password" name="password" id="password" value="" />
                         <div style="clear: both;" >
                             <input type="submit" name="login" value="Login" />
                            </div >
                        </div >
                    </form >
            <?php
    }
        function displayPageHeader() {
    ?>
        </div>

<?php
}
?>

please tell any one y wud it not logout

Recommended Answers

All 11 Replies

session_start() needs to be the first thing.

Shift your PHP above the HTML doctype

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<!--[if lt IE 7 ]> <html class="no-js loading ie6 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 7 ]>    <html class="no-js loading ie7 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 8 ]>    <html class="no-js loading ie8 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js loading" dir="ltr" lang="en-US"> <!--<![endif]-->
<?php
        session_start();
        define( "USERNAME", "john" );
        define( "PASSWORD", "secret" );

        if ( isset( $_POST["login"] ) ) {
            login();
        } elseif ( isset( $_GET["action"] ) and $_GET["action"] == "logout" ) {
            logout();
        } elseif ( isset( $_SESSION["username"] ) ) {
        displayPage();
        } else {
        displayLoginForm();
        }

        function login() {
        if (isset( $_POST["username"] ) and isset( $_POST["password"] ) ) {
            if ($_POST["username"] == USERNAME and $_POST["password"] == PASSWORD ){
                $_SESSION["username"] = USERNAME;
                session_write_close();
                header ('Location: login.php');
                } else {
                displayLoginForm ('Sorry, that username/password could not be found. Please try again.');
                }
            }
        }
        function logout()
        {
        unset( $_SESSION["username"] );
        session_write_close();
        header('Location: loginpage.php');
        }   
        function displayPage(){
            displayPageHeader();
    ?>
    <p> Welcome, <strong > <?php echo $_SESSION["username"] ?>
    </strong > ! You are currently logged in. </p >
    <p> <a href="loginpage.php?action=logout"> Logout </a> </p>

    <?php
        }
        function displayLoginForm( $message="" )
        {
        displayPageHeader();
    ?>

<html>
    <head>
    <meta charset="UTF-8" />
    <title>Student Registration-NPCS</title>
    <meta name="description" content="Management Software National Pre Cadre High School" />
    <meta name="keywords" content="HTML5, Wordpress" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="shortcut icon" href="favicon.png" />
    <link rel="apple-touch-icon" href="img/touch-icon.png" />
    <link rel="image_src" href="img/touch-icon.png" />

    <link rel="stylesheet" href="css/style.css" />
    <script type='text/javascript' src='js/jquery.min.js'></script>
    <script type='text/javascript' src='js/jquery.plugins.min.js'></script>
    <script type='text/javascript' src='js/site.js'></script>

    <!--[if IE]>
    <script type="text/javascript" src="js/ie.js"></script>
    <script type="text/javascript" src="js/respond.min.js"></script>
    <![endif]-->  
   <style type="text/css" >
.error { background: #d33; color: white; padding: 0.2em; }
</style >
    </head>

    <body>
    <div class="border-header"></div>
    <section id="header" class="container">
        <header id="logo" class="grid3 col">
            <hgroup>
                <h1><a class="animated shake" href="loginpage.php" rel="home" title="Main Page!"><img src="img/logo-gray-yellow.png" class="scale" width="180" height="67" alt="CleanGold"/></a></h1>
            </hgroup>
        </header>
        <div class="sep sep-small"></div>

    </section>
    <section id="content" class="container">
        <a class="button big full-width gray animated shake">Management Control System</a>
    </section>
<br>
    <br>
        <br>
            <section id="content" class="container" align="center">
        <div class="grid12 col" align="center" >
            <?php
        if ($message) echo '<p class="error">' .$message. '</p>';
    ?>
                   <form action=" loginpage.php " method="post" >
                       <div style='width: 30em;' >
                         <label for="username" > Username </label >
                         <input type="text" name="username" id="username" value="" />
                         <label for=”password" > Password </label >
                         <input type="password" name="password" id="password" value="" />
                         <div style="clear: both;" >
                             <input type="submit" name="login" value="Login" />
                             <input type="reset" name="reset" value="Reset" />
                            </div >
                        </div >
                    </form >
                    <?php
    }
        function displayPageHeader() {
    ?>

        </div>


</html>
<?php
}
?>

please check i have changed the code now the login and log out is working but logout shifts it to another page instead of the desired one loginpage.php

it takes it to login.php that exist there tooo

Did you read the post ^^

session_start() has to be the first out put, even before your DOCTYPE decleration.

Amend that first::

well i sorted it out just tell me if i change the login to another page
and then i paste the logout code in tht page would it work???

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<!--[if lt IE 7 ]> <html class="no-js loading ie6 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 7 ]>    <html class="no-js loading ie7 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if IE 8 ]>    <html class="no-js loading ie8 oldie" dir="ltr" lang="en-US"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js loading" dir="ltr" lang="en-US"> <!--<![endif]-->

<html>
 <head> 
    <meta charset="UTF-8" />
    <title>Student Registration-NPCS</title>
    <meta name="description" content="Management Software National Pre Cadre High School" />
    <meta name="keywords" content="HTML5, Wordpress" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="shortcut icon" href="favicon.png" />
    <link rel="apple-touch-icon" href="img/touch-icon.png" />
    <link rel="image_src" href="img/touch-icon.png" />

    <link rel="stylesheet" href="css/style.css" />
    <script type='text/javascript' src='js/jquery.min.js'></script>
    <script type='text/javascript' src='js/jquery.plugins.min.js'></script>
    <script type='text/javascript' src='js/site.js'></script>

    <!--[if IE]>
    <script type="text/javascript" src="js/ie.js"></script>
    <script type="text/javascript" src="js/respond.min.js"></script>
    <![endif]-->  


 </head>
 <body>
    <div class="border-header"></div>
    <section id="header" class="container">
        <header id="logo" class="grid3 col">
            <hgroup>
                <h1><a class="animated shake" href="index.html" rel="home" title="Main Page!"><img src="img/logo-gray-yellow.png" class="scale" width="180" height="67" alt="CleanGold"/></a></h1>
            </hgroup>
        </header>
        <nav id="menu" class="grid9 col clearfix">
            <ul>
                <li class="current_page_item arrow">
                    <a href="index.html">Student</a>
                    <ul class="sub-menu">
                        <li class="current_page_item"><a href="Stdnt_reg.html">Register &nbsp Student</a></li>
                        <li><a href="index2.html">Entry Test</a></li>
                        <li><a href="index3.html">Admission</a></li>
                        <li><a href="index4.html">Fee</a></li>
                        <li><a href="index5.html">Annual Charges</a></li>
                                                <li><a href="index5.html">School Leaving Certificate</a></li>
                    </ul>
                </li>
                                <li class="arrow">
                    <a href="#">Accounts</a>
                    <ul class="sub-menu">
                        <li><a href="about.html">Student Record</a></li>
                        <li class="arrow">
                            <a href="#"> Fee Defaulter</a>
                            <ul class="sub-menu">
                                <li><a href="#">Class List</a></li>
                                <li><a href="#">Section List</a></li>
                                                                <li><a href="#">Search & Pay</a></li>
                                                                <li><a href="#">List All</a></li>
                            </ul>
                        </li>
                        <li class="arrow">
                            <a href="#"> Daily Record</a>
                            <ul class="sub-menu">
                                <li><a href="#">Fee Collection</a></li>
                                <li><a href="#">Inventory Sold</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li class="arrow">
                    <a href="#">Inventory</a>
                    <ul class="sub-menu">
                        <li><a href="typography.html">Full Course</a></li>
                        <li><a href="grid.html">Books</a></li>
                        <li><a href="forms-buttons.html">Copies</a></li>
                        <li><a href="alerts-widgets.html">Accessories</a></li>
                    </ul>
                </li>
                <li><a href="portfolio.html">Results</a></li>
                <li class="arrow">
                    <a href="#">Request</a>
                    <ul class="sub-menu">
                        <li><a href="typography.html">Books</a></li>
                        <li><a href="grid.html">Copies</a></li>
                        <li><a href="forms-buttons.html">Stationery</a></li>
                        <li><a href="alerts-widgets.html">Accessories</a></li>
                    </ul></li>
                                <li><a href="journal.html">Salaries</a></li>
                    </form>
            </ul>
        </nav>
        <div class="sep sep-small"></div>
        <span id="switch">Menu <strong>&#8801;</strong></span>
    </section>
                <section id="content" class="container">
        <div class="col sep sep-big"></div>
        <div class="grid12 col">
            <div class="call-to-action clearfix">
                <div class="left">
                    <h3>Hello, Did You Submited the Daily Reports?</h3>
                    <p>If Not then please Submit Them before Leaving .</p>
                </div>
                <a href="#" class="button right animated shake">Submit Reports</a>
            </div>
        </div>

        <div class="col sep sep-big"></div>
        <div class="grid3 col">
            <h2>Quick Links</h2>
            <p class="small"> Use these links to Quickly Move through Pages</p>
        </div>
        <div class="grid9 col add-bottom">
            <div class="grid3 col alpha">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/reports.png" alt=""/></a>
                </div>
            </div>
            <div class="grid3 col">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/register.png" alt=""/></a>
                </div>
            </div>
            <div class="grid3 col omega">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/fee.png" alt=""/></a>
                </div>
            </div>
            <div class="grid3 col alpha">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/default.png" alt=""/></a>
                </div>
            </div>
            <div class="grid3 col">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/copies.png" alt=""/></a>
                </div>
            </div>
            <div class="grid3 col omega">
                <div class="centered-image animated shake">
                    <a href="#"><img src="img/acs.png" alt=""/></a>
                </div>
            </div>
        </div>
        <a href="#top" id="top-link"><span></span></a>
    </section>
            <footer id="footer">
        <div class="sep-big"></div>
        <section class="container">
            <div class="grid4 col">
                                   <a href="experiment.php?action=logout" class="button right animated shake" >logout</a>
                </div>

        </section>

    </footer>
 </body>
</html>


would it be like this or some thing else ???

I support Squidge for his replies earlier additional to that in your logout function I saw the culprit that intends not to work, so what I did is relocate the header(location:loginpage.php); I remove this from the function tentatively.

I can't distinguish where is your logoutpage.php, but I assume that the code you are posting is the logoutpage.php already

then I finally saw the logout link's code having an href called experiment.php?action=logout, in the manner of your href... you are requesting the url to proceed to logout process file having to show a variable named as action with a value that is 'logout' then you'll have to perform an if block.

place this code below your session variables of your php.

<?php

//write this code under your session start and session variables
if((isset($_GET['action']))&&($_GET['action']="logout"))
{
header(location:loginpage.php);
}
?>

ok i started a session in the on the redirected page from the login and placed the logout code in it and should i remove href from the logout button??

if i just puch the back arow of the browser instead of loging out it takes me to the experiment.php page and there i c another page where it tells that im logged in and and a welcome message as well

You should call session_destroy() to log out. From your latest piece of code it doesn't look as if you are including your PHP script on the page. As the guys above have been saying, put it at the very top before any HTML or anything. session_start() along with other functions require that nothing has been output to the browser at all in order to work. If you try to use them afterwards you will likely get a "headers have already been sent" error.

Your Session_start() function should be placed above the doctype declaration.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.