How can I add a URL to a query string? - php

I have a small problem with my PHP script. I want to be able to have a URL within a query string so it would look like this:
http://example.com/?url=http://google.com/
This works absolutely fine and $_GET['url'] will return http://google.com.
The problem is when the URL in my query string already has query string, for example:
http://example.com/?url=http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
will return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8
and I want it to return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
I am using PHP for server side.
Could anybody please help?
Update
I am using Codeigniter, so if this is the reason why it isn't working as it should then please let me know.

You need to encode the url passed as query argument:
If you send it from PHP, use urlencode or rawurlencode.
If you send it from JS, use encodeURIComponent.

Use urldecode() to pass query string

Related

Redirecting to a url by appending query strings to it

I have to capture a query string at the starting of my survey, which i was able to do using php syntax. I now have the query string sitting in php.
My next task is to redirect to another URL at the end of my survey, BUT by appending the same query string I captured before.
For example,
If I have a php field $user='abcdefgh';
And
I need to redirect to another link at the end, say https://en.wikipedia.org/wiki/Stack_Overflow
By adding the query string $user. So the URL now becomes
https://en.wikipedia.org/wiki/Stack_Overflow?id=abcdefgh
Can someone guide me how to accomplish this.
I tried to do it using header in php, but it doesn't seem to work.
Any assistance will be greatly appreciated.
Neeraj
You can try something like this:
$url = https://en.wikipedia.org/wiki/Stack_Overflow;
$user='abcdefgh';
header("location:".$url."?id=".$user);

Encoding string in PHP

I currently working on PHP and at some part of code I generate URL string. I set the GET parameter to currencyCode. But before I add &. So, at result I must get &currencyCode but get ¤cyCode.
How do I fix it?
You need to create url this way using urlencode() function:
<?php
echo 'http://example.com/'.urlencode('&currencyCode');
I also think that your main problem is with displaying it. You should use:
echo htmlspecialchars('&currencyCode');
to display it.
Otherwise it seems browser change the first part of this string into: ¤ entity so you get something like that ¤cyCode and what makes you have display ¤ symbol what is ¤ and the rest of string cyCode
You should use urlencode() function in php to encode the url. Your code should look like this
<?php
echo 'http://yoursitename.com/'.urlencode('&currencyCode');

PHP error using & in url

There is a string XX&YY and I'm passing it to another page. ie, localhost/sample/XX&YY/1 for some processing. Now when I try getting the name value on the other side I'm able to get only XX and not full XX&YY. How to rectify it? Any ideas?
Note : here is my url localhost/sample.php?name=somevalue&pageno=somevalue has been url re-written to localhost/sample/name/pageno.
You have to escape the URL . You can use rawurlencode() or urlencode() to encode your URL.
sidenote: Difference of the 2 functions
If I'm understanding correctly, this is the URL to your script:
http://localhost/sample/name/pageno
Which is then rewritten by your web server to this:
http://localhost/sample.php?name=somevalue&pageno=somevalue
Then, this is how you should format the URL:
$url = sprintf('http://localhost/sample/%s/%s',
urlencode('XX&YY'),
urlencode('1')
);

php file_get_contents encoding issue

I am trying to Integrate an SMS service into my website. I need to make a HTTP call with a param named "msg" (urlencoded).
Currently, I am constructing the entire URL with msg param being urlencoded (i.e $msg = urlencode($msg)), and I am sending the SMS.
$msg = urlencode("Hello World");
Although what I receive on my phone is "Hello+World" and NOT "Hello World".
So is there an issue with file_get_contents ?
Also, is there anyway, I can see the string "file_get_contents" finally sends out ?
Try using rawurlencode() instead.
The principal difference (although there are others) is that it encodes spaces as %20 instead of +. This is quite likely the source of your problem.
I figured what was the issue:
I was using http_build_query to create the query string, and I was passing a urlencode'd param to it. Hence it was being encoded twice.
So is there an issue with file_get_contents ?
No.
Also, is there any way, I can see the string file_get_contents finally sends out ?
Yes, with a network sniffer.
You need to use urldecode() after retrieving data and then display to user

How do I INSERT the character "&" into a MySQL database?

I think I have seen this question before but I don't think it's answered good enough yet because I can't get it to work.
The case:
I want to insert an URL into my MySQL database like so:
$url = $_POST["url"]; //$_POST["url"] = "http://example.com/?foo=1&bar=2& ...";
$sql = mysql_query("INSERT INTO table(url) values('$url')") or die ("Error: " . mysql_error());
Now, the URL is inserted into the database properly but when I look at it, it looks like this:
http://example.com/?foo=1
It's like the URL is cut right at the "&" character. I have tried: mysql_real_escape_string, htmlspecialchars, escaping by doing "\" etc. Nothing seems to work.
I have read that you might be able to do it with "SQL Plus" or something like that.
Thanks in advance.
Regards, VG
Chances are the problem here is nothing to do with the database query, and more to do with how the url is passed to the page. I suspect you'll find that the URL used to load the page is something like:
http://mydomain.com/?url=http://example.com/?foo=1&bar=2
This will result in a $_GET that looks like this:
array (
'url' => 'http://example.com/?foo=1',
'bar' => '2'
)
What you need is to call page with a URL that looks more like this:
http://mydomain.com/?url=http://example.com/?foo=1%26bar=2
Note that the & has been encoded to %26. Now $_GET will look like this:
array (
'url' => 'http://example.com/?foo=1&bar=2'
)
...and the query will work as expected.
EDIT I've just noticed you're using $_POST, but the same rules apply to the body of the request and I still think this is your problem. If you are, as I suspect, using Javascript/AJAX to call the page, you need to pass the URL string through encodeURIComponent().
It is likely the querystring is not being passed. It looks like you are receiving it from a FORM post. Remember that form posts that use a method of GET append a querystring to pass all of the form variables, so any querystring in the action is typically ignored.
So, the first thing to do is echo the URL before you try to INSERT it to make sure you are getting the data you think you are.
If there are variables you need to pass with the URL, use hidden inputs for that, and a method of GET on the form tag, and they will get magically appended as querystring parameters.
Right !! The problem here is nothing to do with the database query has DaveRandom said.
Just use the javascript function "encodeURIComponent()".
Depending on what you want to do with the stored value, you also urlencode() the string: http://php.net/manual/de/function.urlencode.php
Cheers,
Max
P.S.: SQL*Plus is for Oracle Databases.
maybe escape the url with urlencode then you can decode it if you want to pull it out of the db

Categories