views:

262

answers:

3

Hi, I have a problem with php header redirect. I already spent hours trying to fix it. The problem doesn't occur when the bit.ly api is not used in the script, I have no clue why.

<?php
    if (strlen($_GET['url']) > 26) {
        $shortenedURL = $_GET['url'];
        if (isset($_GET['login']) && isset($_GET['apikey'])) {
            $shortenedURL = file_get_contents('http://api.bit.ly/v3/shorten?format=txt&amp;login='.urlencode($_GET['login']).'&amp;apiKey='.$_GET['apikey'].'&amp;uri='.urlencode($_GET['url']));
        }
        else {
            $shortenedURL = file_get_contents('http://icbrd.net/shorten.php?longurl='.$_GET['url']);
        }

        if (strlen($shortenedURL) > 0) {
            header( 'Location: icebird://compose?status='.$shortenedURL.'%20' );
            exit();
        }
        else {
            header( 'Location: icebird://compose?status='.$_GET['url'].'%20' );
            exit();
        }
    }
    else {
        header( 'Location: icebird://compose?status='.$_GET['url'].'%20' );
        exit();
    }
?>

I hope you can help me, as this is driving me crazy. Regards

+1  A: 

You really need to get the hang of ways to debug.

Use a variable to hold the url and use that as the parameter to file_get_contents. This way you can output/debug the value and see what is going wrong.

If the URL looks good then request the URL manually and/or output/debug the $shortenedURL variable to see the contents - it could be an error instead of what you are expecting.

It's near impossible for us to debug your code since we don't know the values to all your variables.

zaf
`var_dump()` is your friend, here.
Adam Backstrom
+1 for teaching how to fish, instead of giving a fish.
chris
@chris thanks..
zaf
I removed the closing php tag, but it's still not working.The purpose of the script is to shorten the specified URL either using icbrd.net or bit.ly if the credentials are part of the request.The URL the script should redirect to will be opened by the iPhone OS.I modified the code to include some echos, here's the output:http://bit.ly/aRvIB6 should have redirectedAs you can see the URL shortening is working, but the redirect is not. If the bit.ly credentials are not part of the request and icbrd.net is used, it works fine, though.It has to be something with the bit.ly call...
Fabian Kreiser
@Fabian Still not enough for us to help you. Have you requested the api manually and checked the output? Have you checked if this is a user agent issue?
zaf
I did that before and saw no problem. However, the bit.ly api returned the URL with a whitespace and I didn't see that using only echo. I'm now using the trim() function to remove it and it works. :)Thanks for your answer
Fabian Kreiser
Then do the decent thing. Click that tick so it goes green.
zaf
A: 

First thing I would do to figure out why the header() redirect isnt working is add echo 'line 2'; on the line after <?php

example:

<?php
  echo 'line 2'; // line 2 so i dont forget where i put the echo later on

This way php will alert you when the headers are being sent because of the 'line 2' text already being sent to the browser.

This may shed some light on the problem.

Jayrox
A: 

Okay guys, it's working now. The bit.ly api returned the url together with a whitespace and from only looking at the echo output in the browser, I didn't realize that. Using trim() now to remove it. Thanks for your answers! :)

Fabian Kreiser