2012-05-10 99 views
-1

這種形式不提交,但我試圖讓它重定向到一個頁面,如果密碼是正確的,如果沒有,然後轉到主頁。我也試圖記住我的Cookie功能。一點幫助,將不勝感激。PHP登錄表格無數據庫

代碼:

<?php 
    if($logged_in){header('Location: index.php');} 
    function checkLogin(){ 
     /* Check if user has been remembered */ 
     if(isset($_COOKIE['cookpass'])){ 
      $_SESSION['password'] = $_COOKIE['cookpass']; 
     } 

     /* Username and password have been set */ 
     if(isset($_SESSION['password'])){ 
      /* Confirm that username and password are valid */ 
      if($_SESSION['password'] != 'password'){ 
      /* Variables are incorrect, user not logged in */ 
      unset($_SESSION['password']); 
      return false; 
      } 
      return true; 
     } 
     /* User not logged in */ 
     else{ 
      return false; 
     } 
    } 
    if(isset($_POST['sublogin'])){ 
     //Set Variables 
     $password = $_POST['password']; 
    if($password == 'password'){ 
     $md5pass = md5($password); 
     $_SESSION['password'] = $md5pass; 
      if(isset($_POST['remember'])){ 
      setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/"); 
     } 
    } 
    } 
    echo 'Not Logged In'; 
    $logged_in = checkLogin(); 
    ?> 

形式:

<form id="form1" name="form1" method="post" action=""> 
    Password: &nbsp;<input name="password" type="password" /><br/><br/> 
    Remember Me: <input type="checkbox" name="remember" id="remember" /> 
    <input name="sublogin" type="submit" value="Login" style="float:right;" /> 

    <br/> 
     <p></p> 
    </form> 

回答

1

你不啓動一個會話。如果您打算在PHP上使用會話變量,則必須在頁面開始處使用session_start()來啓動它們,然後才能發送標題。此外,該表單沒有有效的操作。

閱讀登錄表格上的一些樣本,互聯網上有很多。我認爲你對問題的處理方法不是最好的或最簡單的...

+0

如果我想將它保留在一頁上,我將如何做php自我 – user1345650