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 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?