2

Is there any way to do a redirect in my PHP that is an if statement. So.. only redirect if such and such conditions are true? Also, could I pass variable through that URL for a GET request?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Chris
  • 461
  • 3
  • 9
  • 16
  • [How do I get a link to run a PHP script?](https://stackoverflow.com/questions/5345972/how-do-i-get-a-link-to-run-a-php-script) This could help you out. Look at all the answers. – Felipe Mar 17 '11 at 22:47

3 Answers3

7
if (condition)
{
    header('Location: url');
}

Put this before you output anything on the page. Replace url with the url you wish you redirect to. You an add GET variables the standard way (url?var1=x&var2=y).

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • make sure you use the full url –  Mar 17 '11 at 22:47
  • What if my if (condition) is located further down the page. I need it to work AFTER i declare certain variables – Chris Mar 17 '11 at 22:50
  • You can put it after your variable declarations. – Gazler Mar 17 '11 at 22:51
  • 1
    And you "have" to put `exit` behind the header, so it redirects immediately after you send the header. Otherwise the script will continue to execute. – smottt Mar 17 '11 at 22:53
  • Chris - you need to use output buffering. With output buffering you can change headers and cookies anywhere in your code (as you did above with redirect). – Christian Mar 17 '11 at 22:55
  • I guess I misunderstood the question. You can do output buffering by calling ob_start(); at the start of your script and ob_end_flush(); at the end. – Gazler Mar 17 '11 at 22:57
  • How do I add a variable into the url of the header URL? – Chris Mar 17 '11 at 23:00
  • @Chris, sorry, I am unsure of what you are asking, please clarify with the url you wish to redirect to. I can only guess you mean "http://example.com/index.php?page=1&category=4" in which case you would literally pass the variables into the query string. – Gazler Mar 17 '11 at 23:02
  • header('Location: index.php?test=$test'); when I echo that, all I get is '$test' instead of the actual value – Chris Mar 17 '11 at 23:03
  • Chris, if you use single quotes then expressions are not evaluated. You can either do: header('Location: index.php?test='.$test) or header("Location: index.php?test=$test") – Gazler Mar 17 '11 at 23:05
  • you should use the full url, not relative for the location header, as per the specs. –  Mar 17 '11 at 23:07
1
function redirect($url) {
    header("location: " . $url);
}
Abdo-Host
  • 2,470
  • 34
  • 33
  • 2
    There should be an exit() or die after the header because some browsers or crawlers ignore the header function which might result in them being able to access unauthorized content. – Gilles Lesire Mar 30 '16 at 08:19
-1

It's very useful redirect function..

function RedirectURL($url, $refreshtime = null)
{
    if(isset($refreshtime))
    {
        echo header('refresh:'.$refreshtime.';url='.$url);
    }
    else
    {
        echo header('Location:'.$url);
    }
    return $url;
}

RedirectURL('login.php' , 5);

RedirectURL('index.php');

or you can assign the header to the variable within the function like this.

function RedirectURL($url, $refreshtime = null)
{
    if(isset($refreshtime))
    {
        $header = header('refresh:'.$refreshtime.';url='.$url);
    }
    else
    {
        $header = header('Location:'.$url);
    }
    return $header;
}

echo RedirectURL('login.php' , 5);

echo RedirectURL('login.php');
Koray
  • 11
  • 5
  • 1
    Please explain that further - to me, that looks like pretty strange code, as for example `header` does not return anything that could be echo'ed – Nico Haase Mar 22 '19 at 09:26
  • A very simple code. There are two parameters for the function, one is optional. You type and run url into the function. If you want to redirect after a few seconds, you enter a number in seconds as the second parameter. I've already given two examples at the bottom of the function. – Koray Mar 22 '19 at 10:07
  • Well, then please explain further what it does: `echo` on the return value of `header` makes not sense. Additionally, why does that function return the unchanged input value? Finally, relative redirects are discouraged, absolute ones should be favored – Nico Haase Mar 22 '19 at 10:09
  • There is a control in the function, if only the url parameter is entered, the call is redirected, if the refresh parameter is entered with the url parameter, the given url is redirected after holding the refreshing value for seconds. – Koray Mar 22 '19 at 10:14
  • And why do you use `echo`? – Nico Haase Mar 22 '19 at 10:17
  • ...additionally, `$refreshtime` is always defined, so the first branch is always executed. You should provide a better check than `isset` for that – Nico Haase Mar 22 '19 at 10:18
  • You don't have to, you can change the function, you can do the echo out of the function. You can assign it to the variable and make an echo later. You can use like echo RedirectURL('index.php'); – Koray Mar 22 '19 at 10:20
  • Why should one echo a static input? – Nico Haase Mar 22 '19 at 10:23
  • Sorry, I don't want to be rude, but you should have a look at the issues and check whether they should be solved. Prior to that, I would not recommend to use that code – Nico Haase Mar 22 '19 at 10:24
  • Yes defined but i ask if isset defined value. – Koray Mar 22 '19 at 10:34
  • no problem, i can edit code your wish if u want. – Koray Mar 22 '19 at 10:36
  • Ok, i edited code, check please.. – Koray Mar 22 '19 at 10:41
  • Sorry, but that code is still horribly wrong: it still uses only the first branch (as `$refreshtime` is **always** set!), and there is still nothing to be returned from `header` as that function has **no return value**. Have you even checked what your code does before posting it? – Nico Haase Mar 22 '19 at 10:43
  • Yes, i checked now online, fine working, what kind of error you receive? – Koray Mar 22 '19 at 10:46
  • And you've checked both cases? What **exactly** is the return value of `RedirectURL`? – Nico Haase Mar 22 '19 at 10:48
  • As you can see at http://sandbox.onlinephpfunctions.com/code/e326077ed71068ff0fb865a1b9458f95b57ec570, both executions contain the `refresh` part – Nico Haase Mar 22 '19 at 10:49
  • Change $refreshtime = 0 to $refreshtime = null please. You are right, i see bug now, but so it works that. – Koray Mar 22 '19 at 10:54
  • How about `if ($refreshtime !== null)` or `if ($refreshtime > 0)`? That should make it run the second part of the conditional in the event that `$refreshtime` has no set value. No need for `isset()` as it's always set even it it is set to a null value. – DonP Nov 27 '20 at 08:15