我在php中创建登录系统。当我填写用户名和密码,会话启动但页面没有重定向到profile.php我想要在登录后登录页面刷新本身。但是当我手动刷新login.php然后它重定向到profile.ph。 我的登录代码是:
if ($result->num_rows != 1) {
//echo "Invalid credentials...!";
$message = "wrong credentials";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
// Authenticated, set session variables
$user = $result->fetch_array();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$message = "You have successfully logged in..";
echo "<script type='text/javascript'>alert('$message');</script>";
redirect_to("profile.php?id={$_SESSION['user_id']}");
}
答案 0 :(得分:0)
使用
redirect("profile.php?id={$_SESSION['user_id']}");
或
http_redirect("profile.php", array("id" => "$_SESSION['user_id']"), true, HTTP_REDIRECT_PERM);
或
header("profile.php?id={$_SESSION['user_id']}");
一些有用的链接
答案 1 :(得分:0)
PHP标头在发送输出后重定向不。而你在这里发送
$message = "You have successfully logged in..";
echo "<script type='text/javascript'>alert('$message');</script>";
根据您的评论,您的功能确实使用了标题重定向。
function redirect_to ($url) { header("Location: {$url}"); }
由于上述原因,这不起作用。
在那里使用javascript重定向。
echo "<script>location.href='profile.php?id={$_SESSION['user_id']}';</script>";
答案 2 :(得分:0)
正如您对问题的评论中所提到的,标题重定向需要是浏览器做的第一件事。所以你需要删除echo语句。
if ($result->num_rows != 1) {
//echo "Invalid credentials...!";
$message = "wrong credentials";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
// Authenticated, set session variables
$user = $result->fetch_array();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
redirect_to("profile.php?id={$_SESSION['user_id']}");
}
答案 3 :(得分:-1)
<?php
ob_start ();
if (isset ( $_POST ['submit'] )) {
$email = $_POST ['email'];
$password = $_POST ['password'];
$sql = "SELECT * from users WHERE email='$email' AND password='$password' limit 1";
$result = mysql_query ( $sql );
$num = mysql_num_rows ( $result );
if ($num > 0) {
while ( $rows = mysql_fetch_array ( $result ) ) {
$username = $rows ['username'];
$uid = $rows ['user_id'];
}
session_start ();
$_SESSION ['user_id'] = $user ['id'];
$_SESSION ['username'] = $user ['username'];
header ( "location:profile.php?id=" . $_SESSION ['user_id'] );
} else {
echo "<p><b>Error:</b> Invalid username/password combination</p>";
}
}
?>
如果您在登录页面本身处理此PHP代码,请使用html提供完整代码。因为自动刷新可能是html代码本身的问题。
答案 4 :(得分:-2)
请粘贴完整代码,否则请尝试替换
redirect_to("profile.php?id={$_SESSION['user_id']}");
使用以下行..
header("location:profile.php?id=".$_SESSION['user_id']);
不要忘记在php代码的顶部添加以下行。
ob_start();