【问题标题】:Redirecting to other PHP page重定向到其他 PHP 页面
【发布时间】:2017-05-01 12:43:02
【问题描述】:

我已经尝试了所有方法,但仍然没有看到像预期的那样重定向到 index.php。 我还添加了 ob_start() 但它不起作用。然后我尝试了 header('location:...')。它也没有工作。然后我尝试使用 JavaScript 进行重定向,但这也没有用。 有什么建议吗??

<html>
<head>    

<link rel ="stylesheet" href=css/style.css>
</head>
<body>
<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start();
//if my form is submitted
if(isset($_POST['submit']))
 {

 $username=$_POST['username'];
 $password=$_POST['password'];

$query1= userIdentity($username,$password);
echo 'returning value deisplay';
  echo '\n' .$query1 . '\n'; 
  echo 'i am here';
  if($query1==1){
  //echo 'welcome dude';
  $_SESSION['username'] = $username;
   //    Redirect user to index.php
   //echo "<script type='text/javascript'>  window.location='index.php';      </script>";
 // header("Location: index.php");
   //e//cho 'heeeelo';
   echo "<script type='text/javascript'>window.location.href = 'index.php';  </script>";
        ob_flush();
        exit(0);       
      }

   // header("Location: index.php");
   else 
   {
   echo  " <div class='form'>
   <h3>Username/password is incorrent.</h3>
   <br/>click here to <a href='login.php'>Login</a></div>";
   } 
   //header("Location: index.php");

  }
  else {

 ?>


 <div class="form">
 <h1>Log In</h1>
 <form action="" method='post' name="login">
 <input type ="text" name="username" placeholder="username" required />
 <input type="password" name="password" placeholder="password" required />
 <input name="submit" type="submit" value="login" />
 </form>
 <p> Not registered yet ? <a href='register.php'>Register Here </a> </p>
 </div>

  <?php
  }
 ?>
 </body>
</html>

【问题讨论】:

  • 不会修复您的代码,但这些 '\n' 应该在双引号内而不是单引号内。
  • 这个userIdentity()函数有什么作用?如果某处出现故障,您需要通过错误报告和查询找出原因,因为它似乎与 db 相关。
  • 我认为单引号也可以
  • 我没有说它不起作用或会抛出错误,它只是回显\n而不是换行。
  • userIdentity() 正在检查用户和密码是否存在 :) 如果返回值为 1,则用户存在,它应该将我重定向到索引 .php 。

标签: php redirect http-headers ob-start


【解决方案1】:

根据header() 的 PHP 手册页:

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是通过 PHP。

因此,将 PHP 代码移到起始 HTML 标记上方,如下例所示。我创建了一个变量来保存消息以防登录尝试失败(即$message)。请注意它最初是如何设置为空白消息的(对于好的scope),然后在适当的时候设置。然后将其添加到下面的表单中。

编辑:

您可以在this phpFiddle 中看到这一点。尝试使用任何用户名登录。如果您输入密码 pw,那么您应该被带到 index.php,该页面上会显示带有文本 phpfiddle.org的图像>。此外,我从else 块中删除了 HTML,因此它始终会显示表单(如果设置了登录失败消息,则插入登录失败消息),但如果登录失败,您可能希望它不再显示表单。 ..

<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start(); 
$message = ''; //initialize to empty, for good scope
//if my form is submitted
if(isset($_POST['submit']))
{

    $username=$_POST['username'];
    $password=$_POST['password'];

    $query1= userIdentity($username,$password);
    if($query1==1){
        $_SESSION['username'] = $username;
        //    Redirect user to index.php</script>";
        header("Location: index.php");
        //...
     }//else:
     else {
          $message = "<div class='form'><h3>Username/password is incorrect.</h3>
          <br />click here to <a href='login.php'>Login</a></div>";
     }
?>
<html>
<head>    
<!-- the rest of the HTML content -->

如果已定义,则在正文中附加该错误消息,例如:

<div class="form"><?php echo $message; ?> 
    <h1>Log In</h1><!-- rest of the HTML for the form-->

【讨论】:

  • 还是不行。当我输入正确的用户名和密码时,我没有被重定向到 index.php。如果我输入正确的密码,则会再次显示登录页面而不是 index.php :(
  • 查看我的更新答案,this phpFiddle
【解决方案2】:

不要在您的 php 代码之前添加 html &lt;html&gt;&lt;head&gt;&lt;body&gt; 标签,而是在这些之前添加 php 代码并使用 header 函数。这是有效的新版本:

<?php
ob_start();
require('dbconnect.php');
require('loginservice.php');
session_start();
//if my form is submitted
if(isset($_POST['submit']))
 {

 $username=$_POST['username'];
 $password=$_POST['password'];

$query1= userIdentity($username,$password);
echo 'returning value deisplay';
  echo '\n' .$query1 . '\n'; 
  echo 'i am here';
  if($query1==1){
  //echo 'welcome dude';
  $_SESSION['username'] = $username;
   //    Redirect user to index.php
   //echo "<script type='text/javascript'>  window.location='index.php';      </script>";
 // header("Location: index.php");
   //e//cho 'heeeelo';
   header("Location: index.php);
        ob_flush();
        exit(0);       
      }

   // header("Location: index.php");
   else 
   {
   echo  " <div class='form'>
   <h3>Username/password is incorrent.</h3>
   <br/>click here to <a href='login.php'>Login</a></div>";
   } 
   //header("Location: index.php");

  }
  else {

 ?>
<html>
<head>    

<link rel ="stylesheet" href=css/style.css>
</head>
<body>



 <div class="form">
 <h1>Log In</h1>
 <form action="" method='post' name="login">
 <input type ="text" name="username" placeholder="username" required />
 <input type="password" name="password" placeholder="password" required />
 <input name="submit" type="submit" value="login" />
 </form>
 <p> Not registered yet ? <a href='register.php'>Register Here </a> </p>
 </div>

  <?php
  }
 ?>
 </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 2018-07-10
    • 1970-01-01
    • 2023-04-09
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多