Jump to content

[SOLVED] whats the best to protect a page


paulman888888

Recommended Posts

I don't know whats the best way to protect a page.

 

I would like to set up a password at the top of my page.

It's not a user sign then can view the page type of think.

Just a quick enter password and then your in type of thing.

It's just for me and my mate.

I don't want to use Mysql.

Link to comment
Share on other sites

OK.....  This is extremely simple.

 

Here's your directory structure:

some_folder:

-some_file.php

-some_other_file.php

-login.php

-logout.php

 

Login could be something like:

 

<?php
session_start(); //initiate the session stuff
$password = 'CorbinIsAwesome';

if(isset($_SESSION['logged_in'] && $_SESSION['logged_in'] == true) {
    echo 'You are already logged in!';
}
else {
    if(isset($_POST['password'])) {
        if($_POST['password'] == $password) { 
            $_SESSION['logged_in'] = true;
            echo 'Correct password!  You are now logged in!';
        }
        else {
            echo 'Incorrect password!';
        }
    }

//heredoc syntax.  Look it up if you don't know what it is

echo <<<HERE
<form action="" method="POST">
Password: <input type="text" name="password" value="" /><br />
<input type="submit" value="Login!" />
</form>
HERE;

}

?>

 

 

 

Then...  At the top of each page you want to protect, you could just do:

 

<?php
session_start();
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != true) {
    require 'login.php';
    exit;
}

//The rest of the content here

?>

 

Then logout.php:

 

<?php
session_start();
if(isset($_SESSION['logged_in'])) unset($_SESSION['logged_in);
?>

 

 

That's not the way I would personally do it (mainly because I don't like the include and exit part), but it's about as basic as it gets.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.