chaiwei Posted November 24, 2008 Share Posted November 24, 2008 Hi all, How to use a urlencode?? I try header('Location='.urlencode('login.php?err=2')); It shows me 404 Not Found. in the url it shows sumthing like this >>login.php%3Ferr%3D2 But if i remove the urlencode header('Location=login.php?err=2'); It works. If I want to use the urlencode? What should I do? And what is the urlencode? As I know urlencode is to convert the special character to ?? format right? But why ppl want to use this function? Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/ Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 I don't think you want to encode the ? as this is useful for the browser (and the php client) to interpret the webpage. There doesn't seem much point encoding this as you are writing the URL. I only encode when you are not sure the content of the URL to avoid ampersand/space issues etc. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697593 Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 According to specification, urls cannot contain several characters (spaces, non ASCII characters, etc). If you try to put such url inside an RSS feed for example, it will not validate. urlencode is a function that replaces those characters with their specific codes, resulting in specification compliant url Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697596 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 Oh, so if I encode the ? it cant works ? But in the browser it shows me this. It automatic decode it back. this make me think it suppose works. NOT FOUND The requested URL /login.php?err=2 was not found on this server. But in the URL it still shows me >>login.php%3Ferr%3D2 Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697603 Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 Yes, it's how it works. Browsers decode %3F into ? %3D into = , %20 into space etc.. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697605 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 So what should I write in the script if I want to jump to this page( login.php?err=2) the err=2 is use for me to using the Get method to get the value. There is no other way to use the urlencode and the special characters? Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697613 Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 header('Location: login.php?err=2'); Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697616 Share on other sites More sharing options...
thebadbad Posted November 24, 2008 Share Posted November 24, 2008 You only use urlencode() on strings to be used in an URL. E.g. when sending a name containing 'special' characters (like spaces and ampersands) via GET: <?php $name = 'You & Me'; //e.g. retrieved from a database $safe_name = urlencode($name); //Redirect to safe, working URL header("Location: page.php?name=$safe_name"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697624 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 Hi, yes,it is working,(404 not found no more) But the err=2 is not working. Because when the login.php being jump, it will get the err from the url by using get method. by using the urlencode, I think the get functions cant work already. because of the err=2 no more. For example. header('Location:login.php?'.urlencode("err=2&language=")); it become like this, login.php?err%3D2%26language%3D but for this the following get methods work.(without the urlencode) header('Location:login.php?err=2&language='); Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697631 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 Another question, in (index.php) header('Location:'.urlencode($_SERVER['HTTP_REFERER'])); CAN'T WORK. The requested URL /chaiwei/http://192.168.2.3/chaiwei/index.php was not found on this server. header('Location:'.$_SERVER['HTTP_REFERER']); works. return to index.php my question is why using urlencode it can't work. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697653 Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 Why you want to use urlencode so badly? Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697655 Share on other sites More sharing options...
Adam Posted November 24, 2008 Share Posted November 24, 2008 You only use urlencode() on strings to be used in an URL. E.g. when sending a name containing 'special' characters (like spaces and ampersands) via GET: <?php $name = 'You & Me'; //e.g. retrieved from a database $safe_name = urlencode($name); //Redirect to safe, working URL header("Location: page.php?name=$safe_name"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697658 Share on other sites More sharing options...
thebadbad Posted November 24, 2008 Share Posted November 24, 2008 To be more clear, you only use urlencode() on the values in the name and value pairs (page.php?name=value) in a URL. It can also be used on the names, but that rarely happens, since you would just use names without special chars. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697668 Share on other sites More sharing options...
trq Posted November 24, 2008 Share Posted November 24, 2008 For your information also, the http defines that the Location header should be a complete url, not just a page name. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697672 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 the http defines that the Location header should be a complete url, not just a page name. Complete url means like www.xxx.com/xx.php? use this header('Location:www.xxx.com/index.php'); instead of this?? header('Location:index.php'); Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697837 Share on other sites More sharing options...
thebadbad Posted November 24, 2008 Share Posted November 24, 2008 the http defines that the Location header should be a complete url, not just a page name. Complete url means like www.xxx.com/xx.php? use this header('Location:www.xxx.com/index.php'); instead of this?? header('Location:index.php'); Yes. But I would also add http:// in front. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697843 Share on other sites More sharing options...
chaiwei Posted November 24, 2008 Author Share Posted November 24, 2008 Thanks guys for your help and guide. Quote Link to comment https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/#findComment-697855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.