直接页面访问预防[关闭]

on my site i have 2 pages, a simple password protected page called login.php and the main webpage called index.php

I am trying to prevent users from directly accessing 'index.php'

  1. If a visitor tried to access index.php directly, it would redirect to login.php
  2. If the visitor entered the correct password, it would allow access to the index.php file.

Currently, I'm stuck in a loop.

  1. When i try to access index.php directly, i am redirected to login.php - this is ok
  2. When i enter the password, click enter, I'm sent to index.php then looped back to the `login.php' page

how do prevent this loop please

// index.php
<?php
    if (!isset($include_allowed)){
        die("<meta http-equiv='refresh'content='0;url=\"http://www.myweb.com/login.php'>");
    }
?>

-

// login.php
<?php
    $include_allowed = true;
?>

many thanks (sorry if its confusing)

please note, there is no username, just a basic password form that checks the password matches the variable before sending to the 'index.php' page

if ($pass == "mypassword") {

There are a ton of tutorials on the web about setting up an authentication system for your website. What you are looking for is using sessions.

Heres a good article on it: http://www.phpbuilder.com/columns/user-authentication/Jason_Gilmore05172011.php3

basically, from here (having the login page), you'd want to have something like

if( !isset( $_SESSION[ 'isLogged' ] ) ) {
    header( 'Location: login.php' );
}

in your index.php

More Links:

http://www.techrepublic.com/forum/questions/101-274721/good-tutorial-for-authenticated-web-sessions-using-phpmysql

http://www.slideshare.net/chadhutchins/basic-user-authentication-with-php-mysql

http://www.9lessons.info/2009/09/php-login-page-example.html

EDIT:

You can also take a look at: Http Authentication with PHP, if you want to avoid using sessions. This can actually also get rid of your login.php script