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

前往另一个页面时,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;
    }
    ?>
    
    打赏 评论
  • dongmu1914 2018-04-20 04:23

    You're using $_GET["Username"] which will be empty in this example, and then setting $_SESSION["Username"] to the empty variable.

    Also this is a very odd way to do user auth.

    打赏 评论
  • doujuan2688 2018-04-20 04:28

    Change this line of code

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

    Into:

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

    Read more about PHP session here

    打赏 评论

相关推荐 更多相似问题