Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP7中从一页重定向到另一页?_Php_Html - Fatal编程技术网

如何在PHP7中从一页重定向到另一页?

如何在PHP7中从一页重定向到另一页?,php,html,Php,Html,我正在用PHP7和HTML5制作一个电子商务网站。当我输入登录详细信息时,admin.php只会刷新,不会像应该的那样重定向到index.php 我已经尝试过使用实际的cookie,不过出于安全原因,我更愿意只使用会话cookie ADMIN LOGIN.php CODE <?php session_start(); if(isset($_SESSION["manager"])){ header("location: index.php"); exit(); } ?> <

我正在用PHP7和HTML5制作一个电子商务网站。当我输入登录详细信息时,admin.php只会刷新,不会像应该的那样重定向到index.php

我已经尝试过使用实际的cookie,不过出于安全原因,我更愿意只使用会话cookie

ADMIN LOGIN.php CODE
<?php
session_start();
if(isset($_SESSION["manager"])){
  header("location: index.php");
  exit();
}
?>
<?php
if (isset($_POST["username"])&&isset($_POST["password"])){
  $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
  $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);

  include "../storescripts/connect_to_mysql.php";
  //$sqlquery =
  $sql = mysqli_query($con, "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1");
  $existCount=mysqli_num_rows($sql);
  if($existCount == 1){
    while($row = mysqli_fetch_array($sql)){
      $id = $row["id"];
    }
    $_SESSION["id"] = $id;
    $_SESION["manager"] = $manager;
    $_SESSION["password"] = $password;
    header("location: index.php");
    exit();
  } else {
    // code...
    echo 'Invalid Log In Credentials<br><br>';
    echo'<a href="index.php">Click Here To Re-Enter Credentials</a>';
    exit();
  }
}
?>

INDEX.php CODE
<?php
session_start();
if (!isset($_SESSION["manager"])){
  header("location: admin_login.php");
  exit();
}

$managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]);
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);

include "../storescripts/connect_to_mysql.php";
$sql = mysqli_query($con, "SELECT * FROM admin WHERE id='$managerID' AND     username='$manager' AND password='$password' LIMIT 1");
$existCount = mysqli_num_rows($sql);
if ($existCount == 0){
  header("location: ../index.php");
  exit();
}
?>
ADMIN LOGIN.php代码

您必须更改重定向呼叫。我建议您在函数中重新封装该逻辑

函数重定向到($uri)
{
header('Location:'.$uri);//重定向到哪里。
http_response_code(301);//重定向的响应代码。
exit();//脚本结束
}
然后在客户端代码中,重定向到index.php

重定向到('/index.php');//注意点的移除。您的文件必须是相对于文档根的,而不是相对于文件系统的。
完成了


附言:作为旁白,不要把这当作人身攻击;这不是用PHP编写代码的正确方法。请看:。现在必须使用适当的HTTP抽象。检查Zend Diactoros.:)

您必须更改重定向呼叫。我建议您在函数中重新封装该逻辑

函数重定向到($uri)
{
header('Location:'.$uri);//重定向到哪里。
http_response_code(301);//重定向的响应代码。
exit();//脚本结束
}
然后在客户端代码中,重定向到index.php

重定向到('/index.php');//注意点的移除。您的文件必须是相对于文档根的,而不是相对于文件系统的。
完成了


附言:作为旁白,不要把这当作人身攻击;这不是用PHP编写代码的正确方法。请看:。现在必须使用适当的HTTP抽象。检查Zend Diactoros.:)

由于某种原因,它似乎仍然不起作用。感谢您的第一个答案。请尝试将响应代码与标题行交换。你有我能看到的错误或堆栈跟踪吗?仍然不起作用。出于某种原因,这似乎仍然不起作用。感谢您的第一个答案。请尝试将响应代码与标题行交换。你有我能看到的错误或堆栈跟踪吗?还是不行。