【发布时间】: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