I'm a little unsure of how to word this one but essentially, I want to achieve the following:
http://my.website/?url=http://another.website/?var1=data&var2=moredata&id=119
And for the URL variable to be: http://another.website/?var1=data&var2=moredata&id=119
Naturally, PHP sees var2 and id as new variables. This would be used to pass a full URL from one page to another, however, it poses an issue when the page already has its own variables in the URL!
Any help appreciated!
You need to encode the secondary url which you're putting inside the url variable when you create it. This will ensure it doesn't contain special querystring characters that the receiving website will misunderstand. If the code in my.website is PHP too then the urlencode function (http://php.net/manual/en/function.urlencode.php) is your friend. For example:
urlencode("http://another.website/?var1=data&var2=moredata&id=119")
produces
http%3A%2F%2Fanother.website%2F%3Fvar1%3Ddata%26var2%3Dmoredata%26id%3D119
which will not be misunderstood by the PHP code reading it as containing further separate variables.
Related
Ho you all, I've got a script in a Wordpress post that sends the value of 4 variable to a URL.
The fact is that since natively WordPress converts & to &, the URL that is meant to recive those variable cannot get them, since the final URL will be
http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
instead of http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
Now I know that it is possible to fix this problem by commenting to lines in wp-includes/formatting.php, but I'm looking for a PHP function that can convert the URL with '&' to an URL with just '&'.
Is it possible? Thanks!
You will need to use htmlspecialchars_decode(). Consider this example:
$url = 'http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4';
$url = htmlspecialchars_decode($url);
echo $url;
// http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
3 quick questions:
I want to create one page "view_build.php" which, when opened, takes a variable from the URL contained within it, and displays unique information for that 'build'. For example
$row[0]
The above is a link to a page called view_build.php?buildname=VARIABLE
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
And if so, is it okay if there are spaces in it? For example:
view_build.php?buildname=Dual Zoren
Lastly, how do I include more than one variable in the URL? What is the syntax?
Thanks so much!
Regards.
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
Yes. To elaborate, if you call view_build.php via an HTTP request with the URL query parameters foo=bar, then within that script, you can access $_GET['foo'] which will have the value bar.
is it okay if there are spaces in it?
No, they should be URL encoded. See http://php.net/manual/function.urlencode.php
For example
<?= htmlspecialchars($row[0]) ?>
Lastly, how do I include more than one variable in the URL? What is the syntax?
URL query parameters are separated by the & character, eg
view_build.php?buildname=Dual+Zoren&foo=1&bar=2
I've been visiting stackoverflow.com for a long time and always found the solution to my problem. But this time it's different. That's why I'm posting my first question here.
The situation looks like this: My website provides a directory explorer which allows users to download whole directory as a zip file. The problem is I end up with error when I want to download a dir containg special characters in it's name, i.e. 'c++'. I don't want to force users to NOT name their folders with those special chars, so I need a clue on this one. I noticed that the whole problem comes down to GET protocol. I use ajax POST for example to roll out the directory content, but for making a .zip file and downloading it I need GET:
var dir_clicked = $(e.target).attr('path'); //let's say it equals '/c++'
window.location = 'myDownloadSite.php?directory_path='+dir_clicked;
I studied whole track of dir_clicked variable, step by step, and it seems that the variable in adress is sent correctly (I see the correct url in browser) but typing:
echo $_GET['directory_path']
in myDownloadSite.php prints
'/c'
instead of
'/c++'
Why the GET protocol is cutting my pluses?
You can use:
encodeURIComponent() //to get the url then use
decodeURIComponent() //to decode and access ur filename.
Use urlencode() and urldecode() on server side.
Try encoding your URI with encodeURI(url) JavaScript function.
window.location = encodeURI('myDownloadSite.php?directory_path=' + dir_clicked);
Maybe use encodeURIComponent() and then remove all %xx occurrences?
When the information is posted it is encoded with special chars, sounds like you just need to decode them before using the information.
You can use php function urldecode() to decode the folder names before using them...
$_GET[directory_path]=urldecode($_GET[directory_path]);
I'm trying to use Reducisaurus Web Service to minify CSS and Javascript but I've run into a problem...
Suppose I've two unminified CSS at:
http:/domain.com/dynamic/styles/theme.php?color=red
http:/domain.com/dynamic/styles/typography.php?font=Arial
According to the docs I should call the web service like this:
http:/reducisaurus.appspot.com/css?url=http:/domain.com/dynamic/styles/theme.php?color=red
And if I want to minify both CSS files at once:
http:/reducisaurus.appspot.com/css?url1=http:/domain.com/dynamic/styles/theme.php?color=red&url2=http:/domain.com/dynamic/styles/theme.php?color=red
If I wanted to specify a different number of seconds for the cache (3600 for instance) I would use:
http:/reducisaurus.appspot.com/css?url=http:/domain.com/dynamic/styles/theme.php?color=red&expire_urls=3600
And again for both CSS files at once:
http:/reducisaurus.appspot.com/css?url1=http:/domain.com/dynamic/styles/theme.php?color=red&url2=http:/domain.com/dynamic/styles/theme.php?color=red&expire_urls=3600
Now my question is, how does Reducisaurus knows how to separate the URLs I want? How does it know that &expire_urls=3600 is not part of my URL? And how does it know that &url2=... is not a GET argument of url1? I'm I doing this right? Do I need to urlencode my URLs?
I took a peek into the source code and although my Java is very poor it seems that the methods acquireFromRemoteUrl() and getSortedParameterNames() from the BaseServlet.java file hold the answers to my question - if a GET argument name contains - or _ they should be ignored?!
What about multiple &url(n)s?
Yes, you need to URL encode your URLs before you submit them as a parameter to another webservice.
E.g.
http://google.com
Becomes
http%3A%2F%2Fgoogle.com
If you do that, no special characters like ?, &, = et cetera survive the process that could confuse the webservice.
(Not quite sure what you're asking with your second question, sorry.)
everything which starts with url is threated as a new url, so you cannot pass a parameter called url2 as a get argument of url1.
Every param name that does not contain a '-' will be treated as input.
So if you do
...?file1=...&url1=...&max-age=604800,
the max-age will not be treated as input.
However,
...?file1=...&url1=...&maxage=604800
here the maxage will be treated as input.
I have an HTML form POSTing to a PHP page.
I can read in the data using the $_POST variable on the PHP.
However, all the data seems to be escaped.
So, for example
a comma (,) = %2C
a colon (:) = %3a
a slash (/) = %2
so things like a simple URL of such as http://example.com get POSTed as http%3A%2F%2Fexample.com
Any ideas as to what is happening?
Actually you want urldecode. %xx is an URL encoding, not a html encoding. The real question is why are you getting these codes. PHP usually decodes the URL for you as it parses the request into the $_GET and $_REQUEST variables. POSTed forms should not be urlencoded. Can you show us some of the code generating the form? Maybe your form is being encoded on the way out for some reason.
See the warning on this page: http://us2.php.net/manual/en/function.urldecode.php
Here is a simple PHP loop to decode all POST vars
foreach($_POST as $key=>$value) {
$_POST[$key] = urldecode($value);
}
You can then access them as per normal, but properly decoded. I, however, would use a different array to store them, as I don't like to pollute the super globals (I believe they should always have the exact data in them as by PHP).
This shouldn't be happening, and though you can fix it by manually urldecode()ing, you will probably be hiding a basic bug elsewhere that might come round to bite you later.
Although when you POST a form using the default content-type ‘application/x-www-form-encoded’, the values inside it are URL-encoded (%xx), PHP undoes that for you when it makes values available in the $_POST[] array.
If you are still getting unwanted %xx sequences afterwards, there must be another layer of manual URL-encoding going on that shouldn't be there. You need to find where that is. If it's a hidden field, maybe the page that generates it is accidentally encoding it using urlencode() instead of htmlspecialchars(), or something? Putting some example code online might help us find out.