How to make a page accessible only when entering PHP

I am currently working on a small project for a dummy login / registration page, and now I want to add a page that is accessible only at login. So the question is how to make a session or cookie and get them? And how to block unregistered users.

I am currently using these codes for login.php and member_area.php: Login.php:

<?php
    session_start();





    if(isSet($_POST['login'])) {
        include('db.php');

        $username = mysql_real_escape_string($_POST['username']);
        $password = sha1($_POST['password'] );

        $query = mysql_query("SELECT * FROM tab WHERE username='".addSlashes($username)."' AND password='".addSlashes($password)."'");
        $res = mysql_num_rows($query);

        if ($res == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['userobj'] = mysql_fetch_assoc($query);

            header('Location: http://localhost/member_area.php');
            exit;
        } else {
            echo 'Data does not match <br /> RE-Enter Username and Password';
        }
    } else {

?>
    <html>
    <head><link rel="stylesheet" type="text/css" href="css.css"></head>
        <body>
                <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
            <table width="200" border="0" cellspacing="1" align="center">
                <form id="form1" method="post" action="login.php">
                <tr>
                    <td colspan="2"><h2>Members login</h2></td>
                </tr>
                <tr>
                    <td>Username: </td>
                    <td>
                        <input type="text" name="username" id="username"/>
                    </td>
                </tr>
                <tr>
                    <td>Password: </td>
                        <td><input type="password" name="password" id="password"/> </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="login" id="login" value="login" />
                    </td>
                </tr>
                </form>
            </table>
            </body>
    </html>
    <?php
    }


?>

Member_area.php:

<?php

?>
<html>
<head><link rel="stylesheet" type="text/css" href="css.css"></head>
    <body>
            <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
        <form action="/Log_out.php" method="get">
            <input type="submit" name="submit" value="Log Out." action="http://localhost/Log_out.php" id="Logout">
        </form>
    </body>
</html>
<?php

?>

Please note that I am completely new to PHP, so there are some directions where you can put the code, if possible, a little explanation.

+5
source share
2 answers

Add this at the top of Member_area.php:

session_start();
if(!isset($_SESSION['username'])){
   header("Location:Login.php");
}

It checks if the session is established or not, if it does not redirect the user to the login page.

+10
source
 <?php
    if(!isset($_SESSION['username'])) {
    die("Please login");
}

?>
<html>
<head><link rel="stylesheet" type="text/css" href="css.css"></head>
    <body>
            <div id="div1">

                <a href="index.php" id="home">Home</a>
                <a href="Login.php" id="login2">Login</a>
                <a href="register.php" id="register">Register</a> 


        </div>
        <form action="/Log_out.php" method="get">
            <input type="submit" name="submit" value="Log Out." action="http://localhost/Log_out.php" id="Logout">
        </form>
    </body>
</html>
<?php

?>

It should be like this:

0
source

All Articles