重定向到其他PHP页面

时间:2016-12-15 19:46:46

标签: php redirect http-headers ob-start

我已尝试过所有方法,但仍未按预期重定向到 index.php 。 我还添加了 ob_start(),但它不起作用。然后我尝试了标题('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>

2 个答案:

答案 0 :(得分:1)

而不是在PHP代码之前使用html df <- data.frame(Type = c("a", "b", "c", "d", "e", "f","g","h","i"), Value1 = c(1, 32, 63, 94, 125, 156,187,218,249), Value2 = c(125, 5, 125, 76, 3, 125,3,2,100), Total = c(126,37,188,170,128,281,190,220,349)) plot <- ggplot(df,aes(x = Type,y=c("Value1","Value2","Total"),fill = EVTYPE))+geom_bar(position="dodge") 标签,在这些之前使用php代码并使用标头功能。这是工作新版本:

<html><head><body>

答案 1 :(得分:1)

根据header()的PHP手册页:

  

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

所以将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-->