dreamevil0002 2014-08-30 12:57
浏览 35
已采纳

php重定向脚本无法正常工作

require "assets/php/config.php";
error_reporting(E_ALL);

$location = null;

if(isset($_GET['l']))
{

$location = retrieveAddress($_GET['l']);
}
if($location != null){

    header('Location: ' . $location);
}
else{
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';
}



public static function retrieveAddress($name)
    {
        $con = new PDO(Config::$host,
                  Config::$user,
                  Config::$password);
        if (!$con) {
    die('Could not connect: ' . mysql_error());
        }

        $sql = $con->prepare('SELECT address FROM links WHERE name = ?');
        $sql->execute($name);
        $location = $sql->fetchAll(PDO::FETCH_ASSOC);

        $con = null;

        return $location;

    }

This script should allow me to go to my database where I save links and then redirect to that location, made it while working on a simple url shortener, I can easily add links so the database configuration works, however when I try to retrieve here nothing appears. I also tried to use error_reporting(E_ALL); but I still get nothing. Anyone got any idea, see anything wrong I fail to see?(I m sure there is something wrong but I can't notice

*Edit: I reach the file via link, no ajax is used. I removed all the text echos and I don t know how to check the value of the $location to see if I'm getting the proper value without using echo, I also edited the mysql to grab only the address, I was also getting the name, which was another mistake, but I seem to have one more somewhere.

  • 写回答

1条回答 默认 最新

  • dongwu5318 2014-08-30 13:05
    关注

    Forget my comment, i just saw you're sending output:

    echo "before all";
    

    before sending your headers. Thats a big no-no. You never send output before redirecting and generally never send headers after output. That includes whitespace as well.

    Additionally, the $location is remaining NULL through all the script since you're changing it from retrieveAddress, yet since your variable is a global you either need to declare it as global $location inside function or access it as $GLOBALS["location"]

    It would be easier if you just returned the $location from function also.

    UPDATE

    Apparently, the error was using public static outside of a class

    public static function retrieveAddress($name)
        {
    

    The result was that somehow server got confused without returning any error or output. The only clue we got (me and OP) was that mysterious message through firebug Reload the page to get source for... One can find more info about this message here.

    So, in case anyone get funny responses from his server, without errors, better check for cases like the above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?