PHP Question - Correct way to $_GET and redirect?

Garrett

music LOUD
Feb 4, 2008
3,847
131
0
Ive been doing it like this and it's not working correctly.

Code:
[b]Destination URL:[/b]
http://blahblah.com/1.php?kw=SEARCHTEXT

Code:
[b]1.php[/b]
<?php
$subid = $_GET['kw'];
header('Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-$subid');

Should I be using urlencode? Thanks guys.
 


There's no need to use urlencode.

Your problem is your trying to put a variable dynamically inside a string with single quotes, which doesn't work. It only works like that with double quotes. Here's what it should look like:

Code:
$subid = $_GET['kw'];
header('Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-' . $subid);

or

Code:
$subid = $_GET['kw'];
header("Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-$subid");
 
  • Like
Reactions: Garrett
There's no need to use urlencode.

Your problem is your trying to put a variable dynamically inside a string with single quotes, which doesn't work. It only works like that with double quotes. Here's what it should look like:

Code:
$subid = $_GET['kw'];
header('Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-' . $subid);

or

Code:
$subid = $_GET['kw'];
header("Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-$subid");


Can you give me another example with the code. what if my affiliate url was http://affiliateurl.com&id=, would it be http://affiliateurl.com&id=$subid, or http://affiliateurl.com&id=adib-$subid
 
Is, for example:

echo "blah blah $blah";

Better/worse/whatever than:

echo 'blah blah ' . $blah;

?

There's no need to use urlencode.

Your problem is your trying to put a variable dynamically inside a string with single quotes, which doesn't work. It only works like that with double quotes. Here's what it should look like:

Code:
$subid = $_GET['kw'];
header('Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-' . $subid);
or

Code:
$subid = $_GET['kw'];
header("Location: http://fhhtf.com/c/?s=19246&c=194125&subid=adon-$subid");
 
Is, for example:

echo "blah blah $blah";

Better/worse/whatever than:

echo 'blah blah ' . $blah;

?

Single quotes are faster, as the interpreter would have to look though the double quoted string for any variable representations VS already knowing that the single quote is good to go.

So yes, single quotes are a distinct advantage.
 
The speed difference between single and double quotes is so insignificant that you should really just use whichever you feel comfortable with. When I'm using a regular string variable I tend to use single quotes, but if I'm concatenating I prefer double:

$str = 'some string';
$concat = "some {$other} string";

To me that looks the cleanest, and adding the {} around the variable lets me also use class variables or arrays if I need to (which I usually do).
 
it's obviously a matter of opinion but Concatenation is faster in speed. Though only fractions of a second on a small script, a loop of 10,000,000 would make a huge difference. Also it's easier to read in my opinion, and it has less room for error in my opinion. It also forces more standards since not many other languages will support variables in strings.