-2
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.

allegroBegin
  • 115
  • 1
  • 10

1 Answers1

3

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.

Community
  • 1
  • 1
  • I tried to both access it as $GLOBALS, declare it and return the $location from the function but still no response. Is there any way to make it give me a response? – allegroBegin Aug 30 '14 at 13:25
  • 1
    Did you made the changes like removing all the text output? If yet, please update the question to know. Are you sure you're getting the correct `$location` after the functin's call? What do you mean response? How actually do you reach the file? link or ajax? –  Aug 30 '14 at 13:30
  • updated now together with all that you asked me but to summarize (I use a link, I'm installing Webug now to debug the php and see if the address is correct. – allegroBegin Aug 30 '14 at 13:42
  • 1
    I don't see any other error except instead `$location` you need to access it with `$location[0]["address"]` –  Aug 30 '14 at 13:48
  • I can't think of any except that `error_reporting(E_ALL);` is giving some error (etc. output), which is highly unlikely since you're only including just a file before that (if that file doesnt exist for example it will give an error). The other thing i can think is some brilliantly hidden _whitespace_ before redirection, which is output. If you're not redirecting at all, what is the response after all, what is the server output? –  Aug 30 '14 at 14:06
  • 1
    Additionally, you want to exit the script by using `return;` or `exit;` _after_ the redirection. –  Aug 30 '14 at 14:15
  • well, what can I say I've done all that and looking at firebug I get no response from the request. Not sure about the procedure on linking somebody, but in case you wish to take a look, in the mean time I'll see if I can find a different way to get the link from my db. http://surl.bluebit.nu/index.php?l=34e5724 (and yes, I checked the parameter does exist in my db and it should return an address) – allegroBegin Aug 30 '14 at 15:17
  • I figured it out, somehow, removing the keywords public static from the function fixed it, it works perfectly now. I however have no idea why, if you know, could you say as an answer, so I could validate it in case anyone else has this? – allegroBegin Aug 30 '14 at 15:46
  • 1
    I absolutely have no idea why do you got that error, though i ran your site against my firebug + NET and got the `Reload the page to get source for..` where your HTML should be. Check **[here](http://stackoverflow.com/questions/8486165/what-can-cause-a-persistent-reload-the-page-to-get-source-for-error-in-firebug)** for this. Glad you solve it :) –  Aug 30 '14 at 15:55
  • Yeah I had the same thing, I ll just put you as an answer, I hope people will scroll down in the mess of comments, thanks again. – allegroBegin Aug 30 '14 at 16:18
  • I got it, static can only be accessed via a class, I was not using one. Documentation huzzah! Can you maybe edit your answer with that? It was the main fix. – allegroBegin Aug 30 '14 at 16:54
  • added some extra info, thanx :) although very weird case. The question in link also doesn't bring any stunning solution to it, though it presents some solutions to search furthermore. –  Aug 30 '14 at 17:38