Jump to content

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/134018-solved-help-urlencode/
Share on other sites

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.

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

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

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;
?>

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=');

 

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.

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;
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.