2013-08-04 381 views
2

我是有一天PHP的初學者。我試圖創建我的PHP登錄代碼,我有一些問題。會議不會保留,如果我去其他網頁上我的網站不記得我登錄。PHP會話登錄

<?php 

    $logincontent; 

    require_once 'config.php'; 

     $logincontent= 
     ' <div class="column-1-3"> 
       <div class="white-box"> 
        <div class="box-title"> 
         Login 
         <div class="subtitle"><img src="images/subtitle-lets-work-together.png" alt="let\'s work together" /></div> 
         <div class="icon"><img src="images/title-icon-contact.png" alt="" /></div> 
        </div> 

        <div class="box-content fixed-height"> 
         <form action="" method="post" class="contact-form"> 
          <div> 
           <b>Username</b> 
           <input class="text-input" type="text" name="username" value="" onfocus="clearInput(this);" onblur="restoreInput(this);" /><br/> 
           <b>Password</b> 
           <input class="text-input" type="text" name="password" value="" onfocus="clearInput(this);" onblur="restoreInput(this);" /><br/>         
           <div class="align-right"> 
            <span class="blue-button"><span><input type="submit" value="SEND &raquo;" /></span></span> 
           </div> 
           <input type="hidden" name="val" value="checkin"> 
          </div> 
         </form> 
        </div> 
       </div> 
      </div><!--/end .column-1-3 --> '; 



    if (isset($_POST["val"])&&($_POST["val"]=='checkin')) 
    { 
     echo "checkin"; 
     DBConnect(); 
     $username=$_POST['username']; 
     $password=$_POST['password']; 

     $username = stripslashes($username); 
     $password = stripslashes($password);  

     $username = mysql_real_escape_string($username); 
     $password = mysql_real_escape_string($password);  

     $sql="SELECT * FROM users WHERE username='$username' and password='$password'"; 
     $result=mysql_query($sql); 

     // Mysql_num_row is counting table row 
     $count=mysql_num_rows($result); 

     // If result matched $myusername and $mypassword, table row must be 1 row 
     if($count==1) 
     { 
      @session_start();  

      // Register $myusername, $mypassword and redirect to file "login_success.php" 
      @session_register('username'); 
      @session_register('password'); 
      $_SESSION['username'] = $username; 
      $_SESSION['password'] = $password; 


     } 
     else 
     { 
      echo "Wrong Username or Password"; 
      unset($username); 
     }   

    } else 
    if ((isset($_POST["val"]))&&($_POST["val"]=='logout')) 
    { 
     @session_start(); 
     session_destroy(); 
    } else 
    { 

    //echo $logincontent;    
    } 

    //if((isset($username))&&([email protected]_is_registered($username))) 
    @session_start(); 
    //if((@session_is_registered('username'))) 
    if (isset($_SESSION['username']) && isset($_SESSION['password'])) 
    { 
     if (isset($db_conn) == false) 
      DBConnect(); 


     $username=$_SESSION['username']; 
     $password=$_SESSION['password']; 

     //echo $username;  
     //echo $password;  

     $sql="SELECT * FROM users WHERE username='$username' and password='$password'"; 
     $result=mysql_query($sql); 

     $row = mysql_fetch_array($result); 

     $logincontent= 
     ' <div class="column-1-3"> 
       <div class="white-box"> 
        <div class="box-title"> 
         Welcome back! 
         <div class="subtitle"><img src="images/subtitle-lets-work-together.png" alt="let\'s work together" /></div> 
         <div class="icon"><img src="images/title-icon-contact.png" alt="" /></div> 
        </div> 

        <div class="box-content fixed-height"> 
         <form action="" method="post" class="contact-form"> 
          <div> 
           Username: <b>'.$row['username'].'</b><br><br> 
           Name: <b>'.$row['name'].'</b><br>'; 

           //if (mysql_field_len ($row['avatar'])==0) 
           if (isset($row['avatar'])) 
           { 
            $Link='images/no_avatar.gif'; 
           } else 
           { 
            $Link=$row['avatar']; 
           } 

           $logincontent=$logincontent."<center> 
            <div class='avatar-image'><img src='".$Link."' alt='' width='100' height='100'/></div></center><br>".' 
           <div class="align-center"> 
            <span class="blue-button"><span><input type="submit" value="logout" /></span></span> 
           </div> 
           <input type="hidden" name="val" value="logout">'." 

          </div> 
         </form> 
        </div> 
       </div> 
      </div><!--/end .column-1-3 --> "; 


      } 


?>   

回答

2

您需要將session_start();置於項目中每個文件的開頭。

另請注意,mysql_ *函數從PHP 5.5起被刪除,將來會被刪除。您應該使用mysqli_ *或PDO

http://www.php.net/manual/en/book.mysqli.php

http://www.php.net/manual/en/book.pdo.php

+0

它仍然不起作用http://pastebin.com/R3XmW6wu – user558126

+1

你把'session_start();'放在每個文件的開始處嗎?我想這張你發佈的文件將它包含在其他文件中。您需要爲每個文件添加'session_start();'。如果你只有索引,所以只能索引 – DeiForm

+0

這個腳本是在login.php中,我有index.php,template.php和about.php。我需要把session_start();在他們所有? – user558126

2

不要使用session_register(),它的棄用,註冊全局變量時,關閉不工作。只需在PHP文件的頂部開始會話即可。你爲什麼要壓制你的session_start()錯誤?

+0

它仍然不起作用http://pastebin.com/R3XmW6wu – user558126

+0

謝謝你們兩位! – user558126