PHP Variable in URL

webdev2009

New Member
Messages
2
Reaction score
0
Points
0
okay so i have my url which is


Code:
http://www.xxxxxxxx.x10hosting.com/beta/?url=http://www.facebook.com/home.php#/profile.php?id=xxxxxxxxxxxxxx&ref=nf#/profile.php?id=xxxxxxxxxxxxxx&ref=nf


here is the code for the php page

<?php

$url_data = $_GET['url'];
echo $url_data;

?>

when i open the page it comes up with

http://www.facebook.com/home.php

and ends just before the HASH (#)

do i have to encode the url or something? im new to php by the way so please be nice! :biggrin:

many thanks

Darren
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Yes, there are special characters that need to be encoded, namely ampersand (&) and the greater than and less than symbols (<>). You can use the function htmlspecialchars() to encode strings so they are http safe.
 

webdev2009

New Member
Messages
2
Reaction score
0
Points
0
i have corrected it using another method, but thanks for your feedback anyway

:biggthump
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
thanks garrettroyce for the function, it will come in handy for me later on
+REP
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
thanks garrettroyce for the function, it will come in handy for me later on
+REP

Thank you :)

Don't forget about it's counterpart: htmlspecialchars_decode() to do the reverse ;)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
It's already been encoded once, since it's already a URI. I think the only characters we need escaped are ampersands. But that is definitely true for encoding the URI the first time.
I guess it depends on what he's using the URL for. It's not clear what the OP's purpose it. I was assuming he wanted to set the attribute of an element to the URI, in which case he'd want urldecode. If he's printing the URI as content, then you're right; htmlspecialchars() is the way to go. If he wants any of the "&ref=" arguments to be a part of $_GET['url'];, he'll need to use a URI encode function when the "http://www.xxxxxxxx.x10hosting.com/beta/?url=" URI is generated.

If only the OP was more explicit about what zhe was doing and hir overall goal, we could be more helpful.
 
Top