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.

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

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。