3

I call a php script http://site.com/process.php that takes a url as one of its parameters. for=

http://site.com/process.php?for=http://www.anotherwebsite.com

I then do this and try to parse_url() but parse_url() gives a parse error.

$uri = $_SERVER['REQUEST_URI']; // /process.php?for=http://www.anotherwebsite.com
parse_url($uri);

How can I encode the for parameter either on the sending side (in the url) or on the receiving side (php) so that parse_url() understands that it's just a parameter that happens to look like a url?

2
  • You need to URL encode the query string values. Commented Feb 11, 2011 at 12:41
  • What does $_GET['for'] give you? Is it the for parameter you 're interested in? Commented Feb 11, 2011 at 12:46

2 Answers 2

3

Before including your url as a get parameter, use urlencode

$full_url = 'http://site.com/process.php?for=' . urlencode('http://www.anotherwebsite.com');

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.


To reverse the result of urlencode, use urldecode. As mario pointed out in a comment below, $_GET parameters are already urldecoded.

Sign up to request clarification or add additional context in comments.

1 Comment

Learn something new everyday, thanks @mario :) Edited my answer with your information.
2

Well, first you must urlencode() the for= parameter, then in process.php, you can simply do

$url = $_GET["for"];
$url = urldecode($url); // http://www.anotherwebsite.com

Here are the functions: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php

2 Comments

I think the urldecode() is not useful, OP might just have to use parse_url() on $_GET["url"].
Well, it works for me : $url = urlencode("example.com"); // http%3A%2F%2Fwww.example.com print urldecode($url); // example.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.