我已经尝试了所有方法,但仍未按预期看到重定向到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>
根据PHP手册页的header():
请记住,在发送任何实际输出之前,必须通过常规HTML标记,文件中的空白行或从PHP调用header()。
因此,将PHP代码移至起始HTML标记上方,如下面的示例所示。我创建了一个变量来保存消息,以防登录尝试失败(例如$message
)。请注意,首先如何将其设置为空白消息(以达到良好的作用域),然后在适当时进行设置。然后将其添加到下面的表格中。
您可以在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-->
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句