2013-05-14 77 views
0

我的網站上我有2頁,叫login.php一個簡單的密碼保護網頁,並呼籲index.php直接頁面訪問預防

我試圖阻止用戶直接訪問「的index.php」

    在主網頁
  1. 如果訪問者試圖直接訪問index.php,它會重定向到login.php
  2. 如果訪問者輸入了正確的密碼,這將允許訪問index.php文件。

目前,我被困在一個循環。

  1. 當我嘗試直接訪問index.php,我重定向到login.php - 這是確定
  2. 當我輸入密碼,點擊進入,我發送到index.php然後環回到'的login.php頁面

如何防止這種循環,請

// 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; 
?> 

非常感謝(抱歉,如果它的混亂)

請注意,沒有用戶名,只是檢查密碼的變量發送到「的index.php頁面

之前相匹配的基本密碼錶單
if ($pass == "mypassword") { 
+1

你需要[Sessions](http://php.net/manual/en/features.sessions.php)。 – ficuscr

+0

查看我的編輯OP – Ascherer

回答

2

網絡上有很多關於爲您的網站設置認證系統的教程。您在尋找的是使用sessions

繼承人上有一個很好的文章:http://www.phpbuilder.com/columns/user-authentication/Jason_Gilmore05172011.php3

基本上,從這裏(具有登錄頁面),你會希望有一些像

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

index.php

更多鏈接:

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

編輯:

您還可以看看:Http Authentication with PHP,如果你想避免使用會話。這實際上也可以擺脫你的login.php腳本

+0

slideshare其中一個實際上是id開始 – Ascherer

+0

非常感謝,教程很棒,不得不對網站做一些小的修改,但沒什麼大不了。所有工作現在完美:-) –