我正在处理一个数据库,登录过程结束后需要将用户重定向到另一个页面。
我正在使用redirect_to()
函数。代码执行后,它会给我错误
致命错误:调用未定义的函数redirect_to()
以下是我尝试的
if($result){
$num_rows = mysqli_num_rows($result);
if($num_rows == 1){
$found_user=mysqli_fetch_array($result);
redirect_to("main.php");
}
else{
redirect_to("index.php");
}
}
在调用redirect_to
函数之前,您必须定义它
<?php
if($result){
$num_rows = mysqli_num_rows($result);
if($num_rows == 1){
$found_user=mysqli_fetch_array($result);
redirect_to("main.php");
}
else{
redirect_to("index.php");
}
}
function redirect_to($location){
header('Location:'.$location);
}
?>
尝试使用标头:
<?php
/* Redirection vers une page différente du même dossier */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
redirect_to()
不是一个函数。您想使用header("Location: main.php");
由于这是在添加头信息,所以在将任何内容传递到页面之前都需要加载它(我相信),所以在调用header()
之前不要试图将任何内容打印到页面。
如果你想做这样的事情,我建议你使用javascript。
redirect_to
不存在,或者如果存在,则说明它未被正确包含/要求。
添加此项;
if( function_exists('redirect_to') == FALSE ) { //Future proofing...
function redirect_to($where) {
header('Location: '. $where);
exit;
}
}
如果您已经有了输出,我建议您考虑使用输出缓冲。
您可以使用以下标头("Location:….")语法轻松重定向:
header("Location: main.php");
exit;
正如其他人所指出的,您应该首先为您的项目配备此功能。或者,重构代码以使用内置的php函数->http://php.net/manual/en/function.http-redirect.php