I m building a small search script for my website. I need to send data by get method because by POST it will get real messy as I have to show many pages of search results.
So, My question is Can I use get method directly? means do i need to encode url or any other thing ??
I have checked it in modern browsers. It works just fine..
Thanks
Edit:
Urlencode is used when puting variables in url.
I am submitting my search form with method='get' Then I get variable and perform search query and make new page links with variable data.
- Length,Size is not a prob.
U people suggesting I should use urlencode func. while making new links only ???
You can and should use urlencode() on data that possibly contains spaces and other URL-unfriendly characters.
http://php.net/manual/en/function.urlencode.php
You need to URL Encode the parameters on the URL eg http://www.example.com/MyScript.php?MyVariable=%3FSome%20thing%3F.
Be aware that there's a limit to how much data can be sent via GET - more restrictive on older browsers. If I remember correctly, IE6 has a limit of 1024 characters in the URL so if you think you're going to go over that, consider using POST or you may exclude some users.
You should use urlencode($variable) (Link) before sending the variable (even though the browser usually takes care of this) and urldecode ($variable) (Link) after receiving it, this way you can be sure special chars will be treated correctly.
Related
I'm currently developing a table layout.
The tables are using a paginator and a filter function in PHP.
All values are transmitted as GET parameters.
For example, the paginator will use &limit=20&page=5.
The filter is built upon a table row in thead as input fields.
What I mean is that each column has it's own input field.
Once the submit button is clicked, it will pass the data via GET to itself, so the next pageview will query/filter the data correctly.
For example, if I want to filter the postcode the url will be as following:
&limit=20&page=5&postcode=5
Because I'm allowing searches like %5% to show all postcodes where 5 where the result is not limited to 5 only. It will show all data which has a 5 at any spot of the value.
However, if I want to filter the postcodes showing all results with 58, I will type in %58%. As per URL encoding, unfortunately, the URL won't be &postcode=%58% as expected. It will be &postcode=X%.
The question is whether it is somehow possible to get the correct values into the URL?
The problem lays on browser level. If I would change the URL from &postcode=X% to &postcode=%58% directly and hit enter, Chrome would translate it straight away to X%.
Maybe it's possible somehow with meta tags, http headers, or Javascript, etc.
I'm doing it via GET instead of POST because it was - apparently - simpler to integrate with the paginator.
Sorry for my bad English. Any help would be much appreciated.
Thanks a lot.
You should escape the "%" sign itself (that would be "%25"). PHP should be smart enough to decode that automatically.
So &postcode=%58% should become &postcode=%2558%25, which PHP will decode so that $_GET['postcode'] is '%58%'.
You should urlencode your values before inserting them into the params.
Overall though, If you are using mysql I agree with billrichards.
Since you mention %% searches I assume you are using MySQL or another SQL back end to query for the data. In that case I would suggest leaving the querystring always formatted as postcode=58&page=1, and add some other parameter to indicate if it should be a %wildcard% search or exact match, and if the wildcard parameter is there, add the %% on the back end when performing the query.
From what I understand it is possible to get query strings and clean up the url afterwards.
I basically want to send a user a link containing many types of data to build up a special page for them and afterwards clean up the url to hide the mess. I would mainly use the parameters to load certain products etc.
What would be the best way going about this? And is this even possible?
Thank you.
My thought is use two functions/links instead for this. First function/link will be that user will click and store those values in a session and transfer it to other neat URL .
I'm using a javascript step sequencer that records the current user-inputed drum pattern into the URL.
So for example before any user input the url looks like:
http://localhost:8888/member-index.php#/0000/0000/0000/0000/0000/0000/0000
and then if the user inputs a basic drum beat the URL might look like:
http://localhost:8888/member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
So I want to be able to save the user-created patterns to my MySQL database so that user's can save and load beats they've previously created.
Could someone give me a quick example of what the PHP code would look like to save the pages current URL to a database?
EDIT:
People are saying to use $_GET - how would I use this with a URL like mine that is broken up into seven sections with "/" dividing them?
Short Answer
Use $_GET instead.
Long Answer
Retrieving the url with PHP isn't going to include what comes after the #, because that's only sent to the browser and not to the server. As #Kazar says in an answer to a similar question, you could use Javascript and document.location.hash to retrieve the information after the hash and then send it to the server via ajax.
But fortunately there's a much better built-in solution, which is $_GET (documentation here).
Instead of constructing your url thus:
member-index.php#/8020/0808/aaaa/0000/0000/0000/0000
Make it like this:
member-index.php?a=8020&b=0808&c=aaaa&d=0000&e=0000&f=0000&g=0000
Then you can retrieve this information easily in PHP:
$a = $_GET['a'];
$b = $_GET['b'];
...
And then pass it on to the database. (Even better, replace a, b, etc. with whatever the order actually means)
You could use htaccess and url rewriting to redirect all requests to a specific php in which you check the url. see:Rerouting all php requests through index.php
nevertheless I think using get/post or the request body is easier to send your data.
I'm trying to sort out an issue with foreign characters and matching those to a database value.
I've managed to get a match out of the database query as I wanted but now I've run into a different problem and simply don't know why what's happening is happening.
On all pages throughout the site there is a header include which has a input field to search the site.
<form action="/search.php" method="get"><input name="q" type="text" />etc...
My problem query string was this grønhøj. When I enter this string into the input form on the homepage I get taken to the search page with the url like so: search.php?q=gr%F8nh%F8j which doesn't work at the moment.
However if I then re-enter that same search query into the header input when im on the search page the page reloads except the url now looks like this: search.php?q=grønhøj which does work.
If the resulting url would remain the same all the time, then I'd not have a problem, but because its inconsistent I don't know how to provide solutions to both possible versions of the query string.
So I guess I have 2 questions.
1) Why does the url not stay the same when it's using the exact same form to submit the string?
2) how can I manipulate both versions (or stop the different pages resulting in different urls) of the url so that the resulting string is consistent regardless of which version of the url I get?
UPDATE: I found a function to detect utf8 encoding Here which allowed me to switch how I handle the url string depending on which version of the url I get, so now my main issue is fixed.
I would still however like to understand why I get the 2 different url variables from the different pages even though the form is a consistent include across the site. Any ideas?
One way you can solve this issue, is to always decode the query string using urldecode() and then forcefully use urlencode() on it again. This way, if the initial query string was url encoded or decoded, no matter what, it will go through decoding and encoding process again, which will result in the same final query string.
Manual - urlencode
Manual - urldecode
I am trying to get an HTML/PHP script to interpret data sent after the ? in a url. I've seen sites that do this, YouTube being one of them. I thought this was called Post Data (not sure if it is), I've been searching for a few days, and I can find is the PHP $_POST[''] with some HTML forms reading the data from a textbox, but I would like to read directly from the url, EX. www.example.com?ver=1
How would I go about doing this?
What you're looking for is called a query string. You can find that data in $_GET.
print_r($_GET);
If you need access to the raw data (and you probably don't, unless you need multiples for some variable names), check $_SERVER['QUERY_STRING'].
You can't do that in HTML pages. In PHP pages, you can read (and process) the parameters using the $_GET array. This array contains all the things after which come after ? in the URL. Suppose we have a URL like
page.php?a=b&c=d
Then we can access a and c parameters by $_GET['a'] and $_GET['b']. There is also $_POST which works a bit different. You can google it to find out more.