3

我是 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))&&(!@session_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 --> ";


          }   


?>           
4

3 回答 3

2

您需要session_start();在项目中每个文件的开头放置。

另请注意,从 PHP 5.5 起,mysql_* 函数已被弃用,将来将被删除。您应该使用 mysqli_* 或 PDO

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

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

于 2013-08-04T09:26:57.030 回答
2

不要使用session_register(),它已被弃用,并且在全局寄存器关闭时不起作用。只需在 PHP 文件的最顶部开始会话即可。你为什么要压制你的错误session_start()

于 2013-08-04T09:31:26.590 回答
1

一个旧帖子,但可以对其他人有所帮助。
在您要使用会话的每个页面上,使用:

session_start();

然后您可以通过以下方式设置会话:

$_SESSION['user'] = "VALUE";

在任何页面上检查会话是否存在:

if(isset($_SESSION['user'])){
    //SESSION EXIST
}

然后您可以通过以下方式删除会话:

unset($_SESSION['user']);

记得session_start();在每一页上使用。

于 2020-04-24T07:12:50.927 回答