我已经创建了一个登录系统。注册页面的工作原理很像。但是登录页面存在一些问题。当我尝试登录时,它会登录我,但仍保留在同一登录页面上。我的意思是即使登录后登录页面也没有可见的变化。我希望登录成功后进行重定向,我尝试过标题,但出于某些奇怪的原因,它没有用。
我的代码(页面中的PHP):
<?php //Start the Session
session_start();
require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
header("location:redirectafterlogin.php"); //header to redirect, but doesnt work
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "Invalid Login Credentials.";
}
}
?>
页面中的HTML:
<title> Login</title>
<link rel="stylesheet" type="text/css" href="usr_style.css" />
</head>
<body>
<!-- Form for logging in the users -->
<div class="register-form">
<p>Please login to continue</p>
<h1>Login</h1>
<form action="login.php" method="POST">
<p><label>User Name : </label>
<input id="username" type="text" name="username" required placeholder="username" /></p>
<p><label>Password : </label>
<input id="password" type="password" name="password" required placeholder="password" /></p>
<a class="btn" href="register.php">Register</a>
<input class="btn" type="submit" name="submit" value="Login" />
</form>
</div>
</body>
</html>
我在做什么错?
编辑:
Redirectafterlogin.php代码(基本上是留言簿:]:
<body>
<div class="main">
<br>
<div id="title"><h2>Post</h2></div>
<br>
<div id="f">
<form id="form1" name="form1" method="post" action="addpost.php">
<table id="g">
<td>Post</td>
<td><textarea name="comment" placeholder="Please type your post here...." rows=10 cols=50 id="comment"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input id="btn" type="submit" name="Submit" value="Post" /> <input id="btn" type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</form>
</div>
<hr>
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY ID DESC LIMIT 50";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<div id="post_info">
<br>
<div id="author">
Name:
<?php echo htmlspecialchars($rows['name']); ?>
</div>
<div id="time">
Time: <?php echo htmlspecialchars($rows['datetime']); ?>
</div>
<br>
<div id="post">
<pre><?php echo htmlspecialchars($rows['comment']); ?></pre>
</div>
</div>
<br>
<?php
}
mysql_close(); //close database
?>
错误:
警告:session_start():无法发送会话cookie-/ customers / 7 /中已经发送过的标头(输出从/customers/7/e/5/sattapesatta.com/httpd.www/login.php:5开始)第6行的e / 5 / sattapesatta.com / httpd.www / login.php警告:session_start():无法发送会话缓存限制器-标头已发送(输出从/customers/7/e/5/sattapesatta.com/开始第6行/customers/7/e/5/sattapesatta.com/httpd.www/login.php中的httpd.www / login.php:5)1警告:无法修改标头信息-标头已发送(输出始于/customers/7/e/5/sattapesatta.com/httpd.www/login.php中的/customers/7/e/5/sattapesatta.com/httpd.www/login.php:5),第20行
当您想使用函数头进行重定向时,必须正确编写它才能起作用。
header('Location: ...');
如果您不使用大写字母和冒号后的空格,将无法使用。请阅读php.net上的手册。
(编辑)我还在注释中看到尝试添加完整的路径,这不是使其正常工作所必需的。
尝试一下
header(“ Location:redirectafterlogin.php”);出口;
也来自PHP docs
[请记住必须在通过标准HTML标记,文件中的空白行或PHP发送任何实际输出之前调用header()。读取包含,或要求函数或另一个文件访问函数的代码,并在调用header()之前输出空格或空行,这是一个非常常见的错误。使用单个PHP / HTML文件时,存在相同的问题。
最简单的方法:
将其放置在页面中的任何html之前(此代码之前甚至不能包含单个空格)<? php ob_start(); ?>
使用它-区分大小写-<?php header("Location: youraddress.html");?>
<?php ob_end_flush(); ?>
时将其关闭删除标题前的空白和换行符:
session_start();
require('connect.php');
这应该是:
session_start();
require('connect.php'); // whitespace removed
和您的:
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
应该是:
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());