Fabian Kreiser
Fabian Kreiser

Reputation: 8337

PHP header() redirect not working after shortening URL using bit.ly

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&login='.urlencode($_GET['login']).'&apiKey='.$_GET['apikey'].'&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

Upvotes: 0

Views: 967

Answers (3)

Fabian Kreiser
Fabian Kreiser

Reputation: 8337

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! :)

Upvotes: 0

Jayrox
Jayrox

Reputation: 4375

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.

Upvotes: 0

zaf
zaf

Reputation: 23244

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.

Upvotes: 1

Related Questions