doubu5154 2018-04-20 04: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-20 04: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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)