doubu5154 2018-04-19 20:19
浏览 105
已采纳

前往另一个页面时,PHP会话无法识别变量

So I'm making a Login - Successful Login page with PHP, and using MySQL Database. My code successfully checked the Username and Password and only allowed me to head to the next page once they are correct.

However, I cannot print out the Username on Successful Login page. So I'm not sure if my session is running properly or not.

login.php

<!DOCTYPE HTML>
<html>
<?php 
session_start();
?>
<head>
<title>Login</title>
</head>
<body>

<!--<form action ="SuccessfulLogin.php" method = "get"> --> // If I put this in my code, the whole program stops checking Username and Password, and just put me to the next page
<?php
//define variables and set to empty values
 $nameErr = $loginErr = "";
 $Username = $website = $Password = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (empty($_POST["Username"])) {
        $nameErr = "Name is required";
      } else {
        $Username = test_input($_POST["Username"]);  
      }

      if (empty($_POST["Password"])) {
        $passErr = "Password is required";
      } else {
        $Password = test_input($_POST["Password"]); 
      }
    //continues to target page if all validation is passed
    if ( $unameErr ==""&& $passErr ==""){
        // check if exists in database
        $dbc=mysqli_connect('localhost','testuser','password','Project')
        or die("Could not Connect!
");
        $hashpass=hash('sha256',$Password);
        $sql="SELECT * from Members WHERE Username ='$Username' AND Password='$hashpass';";
        $result =mysqli_Query($dbc,$sql) or die (" Error querying database");
        $a=mysqli_num_rows($result);

        if ($a===0){
            $loginErr="Invalid username or password";
        }else{ 
            $_SESSION["Username"]=$Username;
            header('Location: /SuccessfulLogin.php');
        }
    }
}

   // clears spaces etc to prep data for testing
function test_input($data){
    $data=trim ($data); // gets rid of extra spaces befor and after
    $data=stripslashes($data); //gets rid of any slashes
    $data=htmlspecialchars($data); //converts any symbols usch as < and > to special characters
    return $data;
}

?>

<h2 style="color:yellow" align="center"> Login </h2>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" align="center" style="color:#40ff00">

 User Name: <input type="text" name="Username" value="<?php echo $Username;?>"/>
<span class="error">* <?php echo $unameErr;?></span>
<br/><br/>
Password:
<input type="text" name="Password" value=""/>
<span class="error">* <?php echo $passErr;?></span>
<br/><br/>
<span class="error">* <?php echo $loginErr;?></span>

<input type="submit" name="submit" value="Login"/> 
</form>

<!--</form>--> // closing tag of form action SuccessfulLogin.php
</html>

SuccessfulLogin.php

<!doctype html>
<html>
<?php
session_start();
$Username=$_GET['Username'];
$_SESSION['Username']=$Username;
?>
<head>

    <meta charset="utf-8">
    <title>Login Form</title>
    <link rel="stylesheet" href="RegisterLogin.css">
</head>

<body>

<!--<form action ="MemberMenu.php" method = "get">-->

    <h2><?php echo "User $Username LOGGED IN"; ?></h2> // Doesn't print out the $Username

    <p align="center"> <a href="MemberMenu.php"> Click here to be redirected to the menu page </a> </p>


<!--</form>-->
</footer>   
</body>
</html>

展开全部

  • 写回答

3条回答 默认 最新

  • dongxing4196 2018-04-19 20:27
    关注

    you need to check session isset or not.

    Change

    <?php
    session_start();
    $Username=$_GET['Username'];
    $_SESSION['Username']=$Username;
    ?>
    

    With

    <?php
    session_start();
    if (isset($_SESSION['Username'])) {
        $Username=$_SESSION['Username'];
        echo $Username;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 设计一个光控计数器,全部用ttl芯片
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部