简体   繁体   中英

PHP Session won't work after refresh

I've tried adding a PHP session, so they don't have to fill in the same password all the time. If I go to the page and fill in the code it will show the page with the correct session but when I refresh the page the session is gone.

Code of the session:

<head>
    <script>
        { background-color:#87CEFA; }
    </script>
</head>

<?php
    error_reporting(0);
    session_start();

    $_SESSION["pass"] = $_POST["code"];
    $pass = $_POST["code"];

    $con=mysqli_connect("localhost","bb","$pass","bb");
    if (mysqli_connect_errno($con))
    {
        echo "Kan geen verbinding maken, de ingevulde code is verkeerd of de server is      offline!";
        echo 'Hello '.$_SESSION["pass"].'!';
    }

    $result = mysqli_query($con,"SELECT * FROM ftp");
    while($row = mysqli_fetch_array($result))
    {
        $myuser = $row['user'];
        $mypass = $row['pass'];
        $myhost = $row['host'];

        $conn_id = ftp_connect($myhost) or die("Couldn't connect to $myhost"); 
        if (@ftp_login($conn_id, $myuser, $mypass))
        {
             //Path to your *.txt file:
             $file = $ftp_server['DOCUMENT_ROOT'] . "bbbb";
             $contents = file($file); 
             $string = implode($contents); 

             echo $string;
         }
     }

     mysqli_close($con);
?>

Thanks.

On every page load you are running the code $_SESSION["pass"] = $_POST["code"]; . Then you try to echo it like echo 'Hello '.$_SESSION["pass"].'!'; . What you are essentially doing is echoing $_POST["code"] . Only when you submit the form will the $_POST variable be set.

You're overwriting your session variable on each page load. You need to check if the form was submitted before setting your session variable:

session_start();
$_SESSION["pass"] = $_POST["code"];

needs to be

session_start();
if ('POST' === $_SERVER['REQUEST_METHOD']) {
    $_SESSION["pass"] = $_POST["code"];
}
  1. You should check that your server configured correctly. Check session storage and stored data
  2. You should start session before any output (check where you start session and you don't have space after ?>). Overriding of super globals values is bad practise

I'm sorry to say this but you seriously need to read more on php sessions .

I took some liberties rewriting and commenting but if there's anything you don't understand, please ask

combo script

<?php
// protect against any output, leave no spaces or shenanigans before the session_start()
// even warnings and errors
// this is the first thing that has to happen BEFORE ANY OUTPUT
session_start();
// is this a good idea when you're in development
error_reporting(0);
// we don't know if $_POST['whateva'] actually exists
if (isset($_POST['code'])) {
  // this may not be such a good idea...
  $_SESSION['pass'] = $_POST['code'];
}
$pass = isset($_POST['code']) ? $_POST['code'] : '';

$error = '';
try {
  $con = mysqli_connect('localhost','bb',$pass,'bb');// is this barebones MVC? just curious
  if (mysqli_connect_errno($con)) throw new Exception('Kan geen verbinding maken, de ingevulde code is verkeerd of de server is offline! Hello '.$_SESSION['pass'].'!');
  $result = mysqli_query($con,'SELECT * FROM ftp');
  $ftpContent = array();
  while ($row = mysqli_fetch_array($result)) {
    $myuser = $row['user'];
    $mypass = $row['pass'];
    $myhost = $row['host'];
    if (!$conn_id = ftp_connect($myhost)) throw new Exception('Couldn\'t connect to '.$myhost);
    if (@ftp_login($conn_id,$myuser,$mypass)) {
      //Path to your *.txt file:
      $file = $ftp_server['DOCUMENT_ROOT'].'bbbb';// where does this $ftp_server come from????
      $contents = file($file);
      $string = implode($contents);
      $ftpContent[$myuser] = $string;
    }
  }
  mysqli_close($con);
} catch (Exception $e) {
  $error = $e->getMessage();
}
// now output all your stuff here
?>
<!DOCTYPE html>
<html>
  <head>
    <style>body{background-color:#87cefa;}.error{color:#f00;}</style>
  </head>
  <body>
    <?php if ($error) echo '<p class="error">There has been an error: '.$error.'</p>'; ?>
  </body>
</html>

Don't copy paste this, look up what was done and evaluate what will happen, there's also a missing variable so you have to take care of that

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