This isn't the best question ever, but since search engines feel the need to ignore symbols, I have to ask somewhere.
In a link, I'll sometimes see a ?, such as [link]/file.extension?some_type_of_info, or even +,&,=, etc ('best example' of what I mean is youtube videos). What are these called and what do they do? A good site would be great to :)
I am mostly interested because I have a site that loads stuff into a page, and currently the way I allow 'bookmarking' a page (or more important to me, being able to go back a 'page') is use hash values to represent my 'page'.
Ultimately I would like to not have the page refresh, which is why hash values are good, but I'd like alternatives if any (not really what hashmarks are meant for, but mostly different browsers seem to treat assigning the hash values in jquery differently)
Again, sorry this is mostly just a "what is this" question, but if anyone could tell me pros/cons towards using the method in question versus hash values, that would be great also :)
See the url specification, in particular the section syntax components:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
… and the definition of query.
The query component contains non-hierarchical data that, along with
data in the path component (Section 3.3), serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any). The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
Ultimately I would like to not have the page refresh
Use the history API. This is independent of the structure of the URL (other than having to be a URL on the same origin).
The part after the ? is called query string. It's used to pass parameters to a web site. Parameters are separated using the & sign. For example, this would pass parameters to a site:
http://test.site.tld/index.php?parameter=value&another=anotherValue
This would pass the parameters "parameter" (with value "value") and the parameter "another" (with value "anotherValue") to the script index.php.
The + sign is sometimes used to represent a space. For example, "Hello World" could be represented as "Hello+World" or "Hello%20World".
A # sign is used to jump directly to an anchor within the page. For example
http://test.site.tld/index.php#docs
Would jump to the anchor "docs" within the web site.
The ? in a URL introduces the query string, which is data provided to the server. Everything prior to the ? specifies the resource on the server (in theory), and everything after it is additional data.
So for example:
http://example.com/foo/bar/page.php?data=one
http://example.com/foo/bar/page.php?data=two
Both URLs cause the page.php page to be retrieved by the server, and since it's a PHP page, a properly-configured server will run the PHP code within it. That PHP code can access the query string data as one big string via $_SERVER['QUERY_STRING'], or as a series of name/value pairs (if that's what it is, it doesn't have to be) via $_GET['paramname']. Note that that's _GET because query string parameters are GET parameters; POST parameters are sent via a different mechanism (not available for just links; you need a form or similar).
The stuff at the end of the url is a querystring. ? is used to denote the beginning of a querystring, they use key=value pairs seperated by &.
To address your question of whether this can be used for bookmarking, I believe the approach you are currently using with URL hashes (#) is correct.
The ? part is a querystring, GET parameters are sent that way.
The more interesting part of your question is: how can I enable the back-button/history for users in a dynamic website? Check out this library: https://github.com/browserstate/History.js/
It enables you (for newer browsers) to get/set history states. Each dynamic page gets it's own address. For older browsers, there is the hash-bang fallback (#/page/page).
Related
I want to know how this code is working:
memberpage.php?action=admin_mail_list&type=outbox
Yes, memberpage.php is a page but is admin_mail_list&type=outbox a separate page?
If No, what is it then?
If Yes, why is there no file type after the name (I mean .php or .html)?
That link is using the GET method, meaning variables are defined in the URL rather than the PHP code itself.
For example, if you were to run a Google or Bing search, it wouldn't just be:
https://google.com/search
It would be something like:
https://www.google.co.uk/search?q=test
The benefit of using this, is if the page is refreshed or sent to a friend, the variable won't need to be redefined like POST, it's already defined in the URL.
So, for example, you may have :
http://example.com/example?q=test
The /example page would have this PHP code:
echo $_GET['q'];
which would print "test".
See the following pages if you need more help.
http://php.net/manual/en/reserved.variables.get.php
https://www.tutorialspoint.com/php/php_get_post.htm
You're describing two different parts of a URI. This isn't exclusive to PHP, the URI recommendation applies to all websites regardless of their programming language.
The first (memberpage.php) is the path, and W3 describes it like this:
Path
The rest of the URI follows the colon in a format depending on the
scheme. The path is interpreted in a manner dependent on the protocol
being used. However, when it contains slashes, these must imply a
hierarchical structure.
and the second (?admin_mail_list&type=outbox) is the query string and is described like this:
Query strings
The question mark ("?", ASCII 3F hex) is used to delimit the boundary
between the URI of a queryable object, and a set of words used to
express a query on that object. When this form is used, the combined
URI stands for the object which results from the query being applied
to the original object.
Within the query string, the plus sign is
reserved as shorthand notation for a space. Therefore, real plus signs
must be encoded. This method was used to make query URIs easier to
pass in systems which did not allow spaces.
The query string represents some operation applied to the object, but
this specification gives no common syntax or semantics for it. In
practice the syntax and sematics may depend on the scheme and may even
on the base URI.
To put it simply, the path of the URI dictates which script is to be run, and fields in the query string are parameters to use in that script.
If you're familiar with working on the command line it might be easier to think of these parameters like options on a command line utility. A comparable command might look something like this:
$ php memberpage.php --admin_mail_list --type=outbox
It's important to remember that parameters like this aren't necessarily required to access the URI so it's inappropriate to think of these are arguments on a command line. If your script absolutely needs these parameters to function, you must create that logic within the script yourself, as it is not enforced by the URI.
To answer your question directly:
Yes!
Passing different parameters to the URI can lead to wildly different pages.You absolutely should consider different URI's to be different pages because from the perspective of your users and the larger web, they certainly are. Both users and search engines will consider them distinct and so should you.
That means that the "memberpage.php" script takes two parameters via $_GET:
"action" which has the value "admin_mail_list"
"type" which has the value "outbox"
See: http://php.net/manual/en/reserved.variables.get.php
For a given url, I want to get the name-after-hash's age from the database. so for url like thepage.php#Madonna, you'll see "119!".
How can I extract the value after the hash in a url? (i need a safe all-browser-compatible-NON-JAVASCRIPT way). I want to do that like $_GET['after hash'].
The reason I'm not using GET is because I want to use AJAX and jquery's history plugin.
Basically what i want is to use ajax to retrieve data from the server according to the value assigned after the hash.
I don't think it's possible. The string after # is interpreted by the browser as an anchor name within the current page. It is not passed to the server.
Javascript
window.location.hash will give you this value. You can then pass it (via AJAX) to the server for results.
PHP
Check the Fragment of parse_url();
Return Values
On seriously malformed URLs, parse_url() may return FALSE and emit a E_WARNING. Otherwise an associative array is returned, whose components may be (at least one):
scheme - e.g. http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
This question didn't make much sense, you need to clarify it.
You want to hash the URL without Javascript, but in a way that can be used by Ajax?
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 am creating a url link and one of the GET variables has a hash symbol in it. The webpage will not read any data after the hash mark. I cannot take it out for two reasons.
The website database (not designed by me in any way) has hash symbols for various items of data. I have no authorization to edit the database. And I'm sure if I did other things would break.
I cannot edit the webpage of the url. It was designed by someone else and again I don't have any authorization to edit it.
The url looks something like this
www.example.com?datapoint1=abc&datapoint2=#def
where the #def is necessary as the webpage will search the database for this exact string. If I could edit the webpage php I could put the hash in when necessary, but as I said, I don't.
To explain a little further. The user collects data (in a Java app) and the data is put into a long url (like the above example but more complicated)and is automatically emailed to a specific user with this link. The second user clicks on the link and does whatever he/she has to do.
I think the only way is to edit the php or javascript of the webpage. Any ideas would be appreciated.
You'll have to encode the # as %23, so your URL would look like this:
www.example.com?datapoint1=abc&datapoint2=%23def
To make it easier, you could use PHP's built-in urlencode function: http://php.net/urlencode
You need to escape the hash in the url if you don't want it to become the hash part. The urlencoded character for a # is %23.
You can use the urlencode() (php.net doc) in php to escape values in php.
You might also like to know about http_build_query_string() which can generate the url query and encode the values properly from a key value array. Check out the php.net examples for more information.
If you can't access the PHP but can use JS (which is sub-optimal) you could make a small script that rewrites the url when it sees a hash is present (will only work if a hash is never present otherwise)
if(window.location.hash) {
// Hash detected, lets rebuild the url
window.location.href = window.location.href + '%23' + window.location.hash.slice(1);
}
For a given url, I want to get the name-after-hash's age from the database. so for url like thepage.php#Madonna, you'll see "119!".
How can I extract the value after the hash in a url? (i need a safe all-browser-compatible-NON-JAVASCRIPT way). I want to do that like $_GET['after hash'].
The reason I'm not using GET is because I want to use AJAX and jquery's history plugin.
Basically what i want is to use ajax to retrieve data from the server according to the value assigned after the hash.
I don't think it's possible. The string after # is interpreted by the browser as an anchor name within the current page. It is not passed to the server.
Javascript
window.location.hash will give you this value. You can then pass it (via AJAX) to the server for results.
PHP
Check the Fragment of parse_url();
Return Values
On seriously malformed URLs, parse_url() may return FALSE and emit a E_WARNING. Otherwise an associative array is returned, whose components may be (at least one):
scheme - e.g. http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
This question didn't make much sense, you need to clarify it.
You want to hash the URL without Javascript, but in a way that can be used by Ajax?