Html, reading data after '?' - php

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.

Related

How to pass data from url without a ? like domaintools.com/domainname.com using PHP

What is the code in PHP
How to pass data from a URL using a ? is straight forward.
http://localhost/raw.php?topic=news
What is needed is to pass data like domaintools.com does just with the data after the /
news.com/football
news.com/politics
A. domaintools.com/stackoverflow.com
B. gives the whois on file (from domaintools database) and even changing the url to fit there system
C. https://whois.domaintools.com/stackoverflow.com
I have googled many permutations of keywords, php code passing via url not a using a ? and many variations of the such with other terms like $path etc
My guess is we missing something easy and/or we just don't know the right question to ask.
Using "?" is called GET parameter, you can use POST parameters to post data to an endpoint

Saving current page URL to MySQL database

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.

How to capture unset REQUEST values in PHP

I'm really unsure if this is even possible but we have an issue where we control an interface that is having XML posted in to it via HTTP post in the form of www.url.com/script.php?xml=<xmlgoeshere>. That is then URL encoded and passed in to us, and we decode and parse it.
Except I have one client who just refuses to url encode their incoming code, which works fine except for when the XML hits an ampersand, at which point everything is being parsed as an end of the xml variable.
www.url.com/script.php?xml=<xmlstart...foo&bar.../>
The end result being that I have XML being POST/GET'd into the xml variable as normal, and then I lose half of the incoming content because of the ampersand.
Now I know that's expected/proper behavior, my question is, is it possible to capture the &bar.../> segment of this code, so that if we hit a known error I can crowbar this into working anyways? I know this is non-ideal but I'm at my wit's end dealing with the outside party.
UPDATE
Ok so I was totally confused. After grabbing the server variables as mentioned below, it looks like I'm not getting the querystring, but that's because on the query they're submitting it has:
[CONTENT_TYPE] => application/x-www-form-urlencoded
[QUERY_STRING] =>
That being the case, is the above behavior still to be expected? Is their a way to get the raw form input in this case? Thanks to the below posters for their help
You'd be hard pressed to do it, if it's even possible, because the fragments of a query string take the format foo=bar with the & character acting as the separator. This means that you'd get an unpredictible $_GET variable created that would take the key name of everything between the & and the next = (assuming there even is one) that would take the value from the = to the next & or the end of the string.
It might be possible to attempt to parse the $_GET array in some way to recover the lost meaning but it would never be all that reliable. You might have more luck trying to parse $_SERVER ['QUERY_STRING'], but that's not guaranteed to succeed either, and would be a hell of a lot of effort for a problem that can be avoided just by the client using the API properly.
And for me, that's the main point. If your client refuses to use your API in the way you tell them to use it, then it's ultimately their problem if it doesn't work, not yours. Of course you should accommodate your clients to a reasonable standard, but that doesn't mean bending over backwards for them just because they refuse to accommodate your needs or technical standards that have been laid down for the good of everyone.
If the only parameter you use is xml=, and it's always at the front, and there are no other parameters, you can do something like this pseudocode:
if (count($_GET)>1 or is_not_well_formed_xml($_GET['xml'])) {
$xml = substr($_SERVER['QUERY_STRING'], 4);
if (is_not_well_formed_xml($xml)) {
really_fail();
}
}
However, you should tell the client to fix their code, especially since it's so easy for them to comply with the standard! You might still get trouble if the xml contains a ? or a #, since php or the web server may get confused about where the query string starts (messing up your $_SERVER['QUERY_STRING'], and either PHP, the client's code or an intermediary proxy or web server may get confused about the #, because that usually is the beginning of a fragment.
E.g., Something like this might be impossible to transmit reliably in a query parameter:
<root><link href="http://example.org/?querystring#fragment"/></root>
So tell them to fix their code. It's almost certainly incredibly easy for them to do so!
UPDATE
There's some confusion about whether this is a GET or POST. If they send a POST with x-www-form-urlencoded body, you can substitute file_get_contents('php://input') for $_SERVER['QUERY_STRING'] in the code above.
YES, Its possible. Using $_SERVER["QUERY_STRING"].
For your url www.url.com/script.php?xml=<xmlstart...foo&bar.../>, $_SERVER["QUERY_STRING"] should contain, xml=<xmlstart...foo&bar.../>;
The following code should extract the xml data.
$pos=strpos($_SERVER["QUERY_STRING"], 'xml');
$xml="";
if($pos!==false){
$xml = substr($_SERVER["QUERY_STRING"], $pos+strlen("xml="));
}
The problem here is that the query string will be parsed for & and = characters. If you know where your = character will be after the "bar" key then you may be able to capture the value of the rest of the string. However if you hit more & you are going to need to know the full content of the incoming message body. If you do then you should be able to get the rest of the content.

Obtaining form input types in php

Is there a php equivalent to something like this jquery:
var allInputs = $(":input"); allInputs.attr('type');
I need to retrieve the types from each of the post variables sent to a php script but I want to do it without using javascript and/or jquery, I guess it would also be nice to get the other attributes as well (id, class etc). Perhaps I have missed something but I have tried to find the answer to this on the internet in search engines etc and can't even find another question similar to this one!
Thanks for any help and advice.
There is no way to do this without passing the types from jquery or javascript. All php knows is that some strings are coming in.
You can do something like this:
$.post('blah.php',{
var1: 'test',
var1Type: 'text'
...
});
You're pretty confused, PHP is a server side language, any value in the $_POST array is just a value (precisely a string). PHP doesn't have any idea of the input type, it's just an HTTP request with some data in its body.
To be clearer, this is what your webserver sees when you're doing a post request:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
PHP module reinterpret that input as key-values pairs, nothing more.
Everything is sent as a string. You have to know what are you using for and what types sent variables are and validate user input data.

Can I send any character by get method in php?

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.

Categories