douzhun4124 2018-03-15 03:28
浏览 88

多个页面中$ _POST中的数据

I am new to PHP and I wrote scripts for simple login. When successfully login and click the link "back to login", I was not able to have the previous login username filled. I know using $_COOKIE['username'] for the value of username works, but I am wondering why $_POST['username'] does not work? Thank you!

login.php

<!DOCTYPE HTML>
<html>
<head></head>
<body>

<form action="./loginProcess.php" method="post">
  Username: <input type="text" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"><br>
  Password: <input type="password" name="password"><br>
  <input type="submit" name="send">
</form>

</body>
</html>

loginProcess.php

<?php
echo "welcome,  ".$_POST['username'].", login success!!";
echo "<br/><a href='login.php'>Back to login</a><br>";
if(!empty($_COOKIE['lastVist'])){
    echo "your last login time:".$_COOKIE['lastVist'];
    setcookie("lastVist",date("Y-m-d H:i:s"),time()+24*3600*30);
}else{
    echo "you first login time:";
}
setcookie("username", $_POST['username'], time()+24*3600*30);
?>
  • 写回答

2条回答 默认 最新

  • dongpaocuan7498 2018-03-15 03:49
    关注

    A session is a way to store information (in variables) to be used across multiple pages.

    Unlike a cookie, the information is not stored on the users computer and unlike post as it has information for specific request sent by user.

    When we use an application, we open it and do some changes, then we close it. This is much like a Session, so to preserve information we have per session global array in php $_SESSION.

    A session is started with the session_start() function and values are stored in simply associative array fashion $_SESSION['key'] = $value;.

    login.php

    <?php
    session_start();
    ?>
    <!DOCTYPE HTML>
    <html>
    <head></head>
    <body>
    
    <form action="./loginProcess.php" method="post">
      Username: <input type="text" name="username" value="<?php echo isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : ''; ?>"><br>
      Password: <input type="password" name="password"><br>
      <input type="submit" name="send">
    </form>
    
    </body>
    </html>
    

    loginProcess.php

    <?php
    session_start();
    
    echo "welcome,  ".$_POST['username'].", login success!!";
    echo "<br/><a href='login.php'>Back to login</a><br>";
    if(isset($_SESSION['lastVisit'])){
        echo "your last login time:".$_SESSION['lastVisit'];
    }else{
        echo "you first login time:".$_SESSION['lastVisit'];
        $_SESSION['lastVisit'] = date("Y-m-d H:i:s", time());
    }
    
    $_SESSION['username'] = $_POST['username'];
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏