Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP redirect with URL variables

I'm very new to php and trying to build a redirect which uses parameters from a URL which will differ from person to person.

The URL a person will access looks like: www.website1.com/redirect.php?p=p123&r=4&s=567

The p, r, and s will change for each person

I then want to redirect them to some other site that looks like this:

www.website2.com/p123.aspx?r=4&s=567

Here is what I have so far, but it's giving me an error "Cannot modify header information - headers already sent by ..."

<html>
   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);


   ?>
</html>

I would really appreciate the help for a beginner.

Thanks!

like image 478
Rob Avatar asked Oct 24 '25 17:10

Rob


2 Answers

You can't do a redirection when you have been sent a "content" (text, html, space, wherever). You should NOT do this before calling the header() function.

As you can see, you have a "" before calling the header() function.

Change that:

   <html>
   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);


   ?>
   </html>

For that:

   <?php

      $fpvalue  = $_GET['p'];
      $frvalue  = $_GET['r'];
      $fsvalue  = $_GET['s'];

      header("Location: http://website2.com/".$fpvalue.".aspx?r=".$frvalue."&s=".$fsvalue);
      exit;
   ?>
   <html>
   </html>

And remember: Check if there is another previous space or "new line" before the "< ?php " tag.

like image 155
Broda Noel Avatar answered Oct 26 '25 08:10

Broda Noel


The error "Cannot modify header information - headers already sent by ..." caused when you place session_start or php header below other codes, e.g. html then you should change into:

<?php
//your php codes
//....
?>

<!DOCTYPE html>
....etc.

This must work

like image 21
Joe Kdw Avatar answered Oct 26 '25 07:10

Joe Kdw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!