【问题标题】:php redirect_to() function undefinedphp redirect_to() 函数未定义
【发布时间】:2014-09-03 11:57:20
【问题描述】:

我正在处理数据库,我需要在登录过程后将用户重定向到另一个页面。

我正在使用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不存在。
  • redirect_to 不是 PHP 方法,如果你没有在任何地方定义该函数,那么它就不会存在......你从哪里得到这个函数?!
  • 没有同名的内置函数。如果你自己写一个,它超出范围
  • header() 是你要找的函数php.net/manual/en/function.header.php

标签: php


【解决方案1】:

您必须在调用之前定义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);
        }
?>

【讨论】:

    【解决方案2】:

    尝试使用标题:

    <?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;
    ?>
    

    【讨论】:

      【解决方案3】:

      redirect_to() 不是函数。你想用header("Location: main.php");

      由于这是添加标题信息,因此需要在将任何内容传递到页面之前加载它(我相信),因此在调用 header() 之前不要尝试将任何内容打印到页面。

      如果你想做这样的事情,我建议使用 javascript。

      【讨论】:

        【解决方案4】:

        redirect_to 不存在,或者如果存在,则未正确包含/需要它。

        添加这个;

        if( function_exists('redirect_to') == FALSE ) { //Future proofing...
           function redirect_to($where) {
              header('Location: '. $where);
              exit;
           }
        }
        

        如果您已经有输出,我建议您使用output buffering.

        【讨论】:

          【解决方案5】:

          您可以使用以下 header("Location: ....) 语法轻松重定向:

          header("Location: main.php");
          exit;
          

          【讨论】:

            【解决方案6】:

            正如其他人指出的那样,您应该首先为您的项目配备此功能。或者重构您的代码以使用内置 php 函数 -> http://php.net/manual/en/function.http-redirect.php

            【讨论】:

              猜你喜欢
              • 2012-02-09
              • 1970-01-01
              • 2013-01-05
              • 1970-01-01
              • 2015-01-11
              • 1970-01-01
              • 2018-02-07
              • 2019-08-11
              • 1970-01-01
              相关资源
              最近更新 更多