gpt4 book ai didi

php - 登录成功后如何跳转? [PHP]

转载 作者:行者123 更新时间:2023-12-04 00:36:00 27 4
gpt4 key购买 nike

我已经创建了一个登录系统。注册页面就像一个魅力。但是登录页面有一些问题。当我尝试登录时,它让我登录,但它停留在同一个登录页面上。我的意思是即使登录后登录页面也没有明显的变化。我希望它在成功登录后重定向,我已经尝试过 header 但由于某些奇怪的原因它没有用。

我的代码(页面中的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&nbsp;&nbsp; : </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>&nbsp;</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
?>

错误:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 1 Warning: Cannot modify header information - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 20

最佳答案

当你想用函数头做一个重定向时,你必须正确地编写它才能工作。

header('Location: ...');

如果不使用大写字母和冒号后面的空格,是不行的。请阅读 php.net 上的手册.

(编辑)
我还在评论中看到尝试添加完整路径,这不是使其工作所必需的。

关于php - 登录成功后如何跳转? [PHP],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476899/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com