简体   繁体   中英

PHP Session Works Slowly

I've started to learn PHP Sessions recently.That really helped me to do the login properly.

I should give the link to you first: mk-appform.net16.net/login.php(feel free to use as you want,This is a testing.Im able to change the pass as soon as it gets fixed) Username:admin Password:1234 Please test it

The problem is,When you're not logged in and type mk-appform.net16.net/advsearch.php directly in the adress bar,The content of the page that I require login beforehand is visible for a second.Then it redirects to login page.But you know,I would not want this to be shown in any way.It should require login eventually.

Here are the PHP codes of login.php

<?php
   if (isset($_POST['submit']))
   {
     if(isset($_POST['user']) && isset($_POST['password'])) 
     {
         $user = $_POST['user'];
         $password = $_POST['password'];

         if(empty($user) || empty($password)) 
         {
           echo 'Please fill the form';
         } 
         else 
         {

         if($user == 'admin' && $password == '1234') 
          { // check the infos
            session_start();
           $_SESSION['user'] = 'admin';
           $_SESSION['password'] = '1234';
           echo 'Login Succeeded.Now redirecting to panel...';
          header("refresh:2; url=advsearch.php");
         } 
    else 
    {      
          echo 'Invalid Username or Password';
    }
   }
  }


   else 
   {
       echo 'Please use the form';
   }
   }
?>

And ,the code of the content I show after successfully logging in(advsearch.php)

<?php
 session_start(); 

 if(isset($_SESSION['user']) && isset($_SESSION['password']))  
    { 
       if($_SESSION['user'] == 'admin' && $_SESSION['password'] == '1234') 
        {
           header("url=advsearch.php");
        } 
       else 
        {
           session_destroy(); 
           echo 'Redirecting..';
        }
    } 
  else 
    { 

           header("refresh:0; url=login.php");
    }

?>

header redirects aren't instantaneous. It takes a few moments for the browser to start shutting down the connection and initiate the new one. That means any content you output on the page after you output the location header can still be viewed. You have to abort your script after outputting the header. eg

<?php

if (need to redirect) {
    header('Location: login.php');
    echo 'redirecting to login page, please wait ...';
    exit(); // you need this
}

... regular page contents ...

In short, if you don't want something visible to the user, then DON'T output it in the first place. Don't depend on everything working properly (or even fast). They rarely do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM