0

I want to use one URL to redirect users to various outgoing URLs. For example http://example.com/out.php?ofr=2 where ofr will refer to the appropriate URL the user should be redirected to.

I have the following php code for out.php

Is this acceptable, or is there a more efficient way to accomplish this (assuming there were 10 or so different URLs in the below script)?

<?php

$ofr = $_GET['ofr'];

if ($ofr == 1) {
    header('location: http://google.com');
}
elseif ($ofr == 2) {
    header('location: http://yahoo.com');
}
else {
    header('location: http://msn.com');
}

?>

Edit: Looking at switch statements as suggested, I believe it would look like:

$ofr = $_GET['ofr'];

switch ($ofr){
case 1: header('location: http://example_1.com');
break;
case 2: header('location: http://example_2.com');
break;
default: header('location: http://example_2.com');
break;
}

Does that look correct? Thanks!

2
  • well, you could pack the locations in an array, so you have to check only once if the key exists instead of writing tons of if-statements. (btw: switch-statements do exist) Commented Mar 9, 2019 at 19:42
  • @FranzGleichmann I edited what I believe a switch statement would look like. Thanks for the tip. Commented Mar 9, 2019 at 19:51

2 Answers 2

2

First of all I suggest to make a redirect function like so:

function redirect($url)
{
    $baseUri=_URL_;

    if(headers_sent())
    {
        $string = '<script type="text/javascript">';
        $string .= 'window.location = "' . $baseUri.$url . '"';
        $string .= '</script>';

        echo $string;
    }
    else
    {
    if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
        header('Location: '.$_SERVER['HTTP_REFERER']);
    else
        header('Location: '.$baseUri.$url);

    }
    exit;
}

then in a file called redirectFiles.php make array of the urls that you want to redirect:

$redirecUrls = [
  'location: http://example_1.com',
  'location: http://example_2.com',
  'location: http://example_3.com',
]

then make a function to do the redirecting:

function redirectUrls($index){
   if(isset($redirecUrls[ $index])
      return redirect($redirecUrls[ $index])

   return false;
}

After that you can do something like this:

$ofr = $_GET['ofr'];
if($ofr!='')
  redirectUrls($ofr)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. Would this be more effective than using the switch method I added to the original post?
@rivitzs Yes. It's all about clean code. this way you make a (routing like) system. always try to do things in separate and reusable functions. always consider something may go wrong and your code will crash. if you keep your code separated the debugging process will be easier.
@rivitzs if you don't check header sent before it may throw a fatal error that headers are sent before and you can not use header('location everywhere you like. but with my redirect function, it always redirects no matter what. you can delete the base URL variable to suit your need
great, makes sense. Thanks for this and for the explanation.
0

Try something like this:

 <?php
 $ofr=$_GET['ofr'];
 $url_array=array([1]=>http://google.com [2]=>http://yahoo.com);
 foreach($url_array as $key=>$value)
 {  
   if($ofr==$key)  
   header('location: ".$value."');
 }
?>

1 Comment

I see what you're saying. I mislead a bit in my sample code, as the redirects would go to different sites. ofr=1 could go to google.com and ofr=2 could go to yahoo.com. That's my bad... I'll update the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.