在php中,我在尝试从数据库查询时将用户重定向到链接。登录结果后,只显示网址,而不是重定向到网址。
<?php
if(isset($_POST['submitMain']))
{
$username= $_POST['username'];
$password = $_POST['password'];
$_POST['username'] = $_SESSION['username'];
$_POST['password'] = $_SESSION['password'];
$sql = "SELECT * FROM user WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
if($count == 1){
$result = mysql_fetch_contents($result); // get the result set from the query
$url = trim($result['url']); // get the url column's value
if ($url == '') {
echo "No url value was set!";
} else {
$ob_get_contents = 'admin.php';
echo "$ob_get_contents ";
exit;
}
} else {
echo "Wrong Username or Password";
}
}
答案 0 :(得分:1)
$ob_get_contents = 'admin.php';
echo "$ob_get_contents ";
exit;
这部分不起作用。它只是回应'admin.php'。您似乎尝试使用ob_get_contents
函数执行某些操作,但它不能像这样工作。在您的代码中,您只有一个名为ob_get_contents
的变量,它与该函数无关。
此外,ob_get_contents
不是从另一个页面获取信息或执行重定向到另一个页面的正确功能。
要重定向到其他页面,请使用Location
标题:
header('Location:' . $url);
exit;
这将导致浏览器的小往返。基本上,您向浏览器发送一个小结果,告诉它对另一个URL执行第二个请求。但不要担心。这是一种常见的解决方案,尤其是在发布信息后。查看有关Post/Redirect/Get的更多信息。
答案 1 :(得分:0)
要重定向用户,您需要设置重定向标头。
为此,您需要使用header功能:
header("location: your location goes here"); exit();
例如,如果您的网站托管在http://www.example.com,然后重定向到管理区域,您将使用:
header("location: http://www.example.com/admin.php"); exit();
您还需要在重定向标题后退出。