Bryan Spector
Bryan Spector

Reputation: 27

ID variable from URL

Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid

I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.

Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.

Now so far, I know that I have to have something like this will get that affiliate id

$aff_id = $_GET['s1'];

Then I can use that variable to create a link or just redirect it to the next page

differentwebsite.com/?id=$aff_id

My question is, where do I place this code at? $aff_id = $_GET['s1'];

Do I have to make a page called ?s1.php or something?

Upvotes: 1

Views: 2076

Answers (3)

y_s
y_s

Reputation: 138

Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.

Edit:

Do I have to make a page called ?s1.php or something?

You need to make a page that the user will land on when they hit the url: www.mywebsite.com/

I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.

Get the value of "s1" from the url and store it in $aff_id:

$aff_id = $_GET['s1'];

If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.

Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:

header('Location: http://www.differentwebsite.com/?id='.$aff_id); 

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101594

Assuming s1 isn't used anywhere else but just to create a link:

<?php
  $s1 = isset($_GET['s1']) && !empty($_GET['s1'])
      ? $_GET['s1'] // it's populated, use the passed value
      : '';         // default value in case it's not present

  //
  // Maybe check $s1 is indeed valid
  //

  $newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>

Then you can output that link somewhere on the page, like:

<a href="<?= $newurl; ?>">New Url Here</a>

urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.

If you want the concise approach:

<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
  New Url Here
</a>

Upvotes: 1

Leeish
Leeish

Reputation: 5213

You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.

Or, simply put. "differentwebsite.com/?id=$_GET['id']"

I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.

If you are not using this for SQL then no SQL Injection could occur @BlackHatShadow.

Upvotes: 0

Related Questions