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);
}
Related
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'm learning about the function urlencode. Is it possible to use this on a file name? So - when you upload a file to your server and then use that file name later, you would be able to use it in a url?
$promotionpicture=$_FILES["promotionpicture"]["name"];
$promotionpicture=rawurlencode($promotionpicture);
Then later...
$imagesource="http://mysite.com/".$userID."/".$promotionpicture;
I'm trying to do this, but every time I navigate to the picture, i get a "Bad request" from my server. Is there a specific php encode function I should use? Or is this wrong all together? Thanks in advance for you help.
urlencode and similar functions are for making an HTTP friendly URL. You would want to keep the normal filename and then when printing the img src, use urlencode.
Note that this is not really the preferred way to do it as you can run into duplicate filenames and misc security issues. It's better to generate a filename for it using a uuid or timestamp or something, that way you can bypass those types of issues.
Pictures are really just raw data, like any other file. It is possible to do something like what you're doing, but not necessarily advisable.
If you want to do something like that, I recommend instead doing something to strip special characters.
$newfilename=preg_replace('/[^a-zA-Z0-9.]/','',$filename);
(from Regex to match all characters except letters and numbers)
That said, keep in mind what others have said. How will you handle file name collisions? Where will the images be stored and how?
One easy way to do this much more robustly is to store in a database the original file name and the MD5 hash. Save the file by its hash instead of by name, and write a script that retrieves the file by matching the original name to the MD5 using the database. If you store the file type, you can issue correct headers and when the user downloads the file or uses it to embed in a web page, it will retain its original name, or display as expected respectively.
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).
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?