David Jawphan David Jawphan - 3 months ago 11
HTML Question

Check page permission by session?

I included

header.php
to every page for header logo and check page permission by
SESSION
!


  • So if you request main.php without login (session null state), It will display login page and do exit in
    header.php
    for not showing current page content.

  • Its header.php is work for every page except
    login.php
    .Because login page is not show login form



So I want to show login form ,how can I check in
header.php
for its ? Sorry for my poor english :(

login.php

<?php
session_start();
include("header.php");
if($_POST){
//set session logged in
}
?>
<div class="login-box ">
<h3> Log In </h3>
<form method="POST">
<input type="text" name="user" placeholder="Type User Name"><br>
<input type="password" name="pass" placeholder="Type Password"><br>
<input type="submit" name="submit" value="Login" class="button">
</form>
<span id="signup_text">You are not still a member.Click <a href="signup.php">Sign Up</a></span>
</div>





header.php

<?php
session_start();
?>
<div id="header" class="container">
<div id="logo">
<h1>Online Quiz Management</h1>
</div>
</div>
<?php
if (isset($_SESSION['login'])) {
echo "<div id='menu'><ul><li><a href=\"sublist.php\">HOme</a></li><li><a href=\"signout.php\">Signout</a></li></ul></div>";
}
else {
echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
exit;
}
?>
</div>
</div>
?>





main.php

<?php
session_start();
include("header.php");
?>
//show main code if logged in

Answer

Just add a check if the current page is login.php and don't run your block if it is

  else if(!basename($_SERVER['PHP_SELF']) == "login.php") {

             echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
             exit;
        }