dongshi9719 2017-02-04 01:10
浏览 28
已采纳

我试图在成功登录到另一个页面(homeDemo.php)后重定向用户,但它似乎没有重定向。 我正在使用PHP

here is my login.php just a simple to hash the passwords and redirect user upon successful log in.

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $username = mysqli_real_escape_string($db , $_POST['umail']);

  $userQuery = "SELECT username, password FROM users WHERE username = '$username'";
  $result    = mysqli_query($db, $userQuery);
  $queryRow  = mysqli_fetch_array($result, MYSQLI_ASSOC);
  $queryCount = mysqli_num_rows($result);


  $verifyPassword = password_verify($_POST['upassword'], $queryRow['password']);

  if ($verifyPassword){
    $_SESSION['username'] = $username;
    header("Location:homeDemo.php");
  }else{
    echo  "Username Or Password is invalid";
  }
  mysqli_close($db);
}

here is the page the user is reirected to

if (isset($_POST['sendList'])) {
/* $itemName = $_POST['itemName'];
 $itemQuantity = $_POST['itemQuantity'];

print_r($itemName);
print_r($itemQuantity);*/

print_r($_SESSION['username']);
$itemList = $_POST['itemList'];
print_r($itemList);

/* foreach($itemName as $key => $iName ){
   print_r($iName);
   echo " ";
 }
 foreach($itemQuantity as $key=> $iQuantity){
   print_r($iQuantity);
   echo " ";
 }*/
}

<div class="container">
   <form class="col s12" action="" method="POST">
     <div class="center-align">
       <button type="button" name="button" class="waves-effect #ff3d00 deep-orange accent-3 waves-red btn-large addField">Add a new row</button>
     </div>
     <table>
          <thead>
            <tr>
                <th class="center-align" data-field="name">Item Name</th>
                <th class="center-align" data-field="price">Item Quantity</th>
            </tr>
          </thead>
          <tbody class="listWrapper">
            <tr>
              <td>
                <div class="input-field col s6">
                  <input type="text" class="validate" name="itemList[]">
                </div>
              </td>
              <td>
                <div class="input-field col s6">
                  <input  type="text" class="validate" name="itemList[]">
                </div>
              </td>
            </tr>
            <tr>
              <td>
                <div class="input-field col s6">
                  <input type="text" class="validate" name="itemList[]">
                </div>
              </td>
              <td>
                <div class="input-field col s6">
                  <input  type="text" class="validate" name="itemList[]">
                </div>
              </td>
            </tr>
          </tbody>
      </table>
      <div class="center-btn">
        <div class="input-field col s12">
          <select class="" name="shopCenters" multiple>
            <option value="" disabled select>Select Your Preferred Shopping Center</option>
            <option value="Shoprite">Shoprite Accra Mall</option>
            <option value="MaxMart">MaxMart Osu</option>
            <option value="Marina">Marina Mall</option>
            <label for="">Shopping Center</label>
          </select>
        </div>
        <input type="submit" class="btn #ff3d00 deep-orange accent-3 center-align" name="sendList" value="SEND">
      </div>
   </form>

and my authentication file to prevent users from accessing the homeDemo.php file if not logged in

<?php
if(!isset($_SESSION['username']))
{
        session_start();
        header('location:Login.php');
        exit;
}
 ?>
  • 写回答

1条回答 默认 最新

  • doufu2496 2017-02-04 01:18
    关注

    Make sure session_start() is before $_SESSION['username'] otherwise $_SESSION['username'] will not have a value.

    You didn't mention, but I bet you were redirected to the login page again.

    <?php
    session_start();
    
    if(!isset($_SESSION['username']))
    {       
        header('location:Login.php');
        exit;
    }
    ?>
    

    Make sure you call session_start() before using the session data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算