PHP/Javascript, Real Filename from Rewritten URL - php

I have a Javascript bookmarklet that POSTs to a PHP script, and I need PHP to know the filename of the page that the bookmarklet is POSTing from. I'll be using that filename with file_put_contents(), so I need it to be a real file.
The PHP will also be running on the same server as the page being POSTed from.
For example:
script.php is placed in the root of an arbitrary server (http://site.com/)
The operator browses to an arbitrary page on the server (http://site.com/foo/bar/baz) before executing the bookmarklet.
The bookmarklet sends some data to http://site.com/script.php.
script.php now needs to make some modifications to the file corresponding to http://site.com/foo/bar/baz, but can't resolve the URL to a filename.
There's location.href in Javascript and $_SERVER[ 'HTTP_REFERER' ] in PHP, but in the case of a rewritten URL (via mod_rewrite or whatever) these won't contain the real filename.
Is there any way to find the real filename of a URL in PHP or Javascript?

There's location.href in Javascript and $_SERVER[ 'HTTP_REFERER' ] in
PHP, but in the case of a rewritten URL (via mod_rewrite or whatever)
these won't contain the real filename.
Is there any way to find the real filename of a URL in PHP or
Javascript?
In Javascript: Client side Javascript has no idea about server side code unless the server passes that information to the client. Short answer: not unless you make it possible.
In PHP: I think that realpath will translate a URL to a physical file on the local drive (in ASP it is Server.MapPath), BUT, I don't know if that will work for rewritten URLs. I very much doubt it. If I am correct that it will not, then there could possibly be a related function specific to whatever framework you are using, if one is being used. If you are simply using mod_rewrite or similar without a framework, then you would have to code up your own function which knows how to translate from the virtual path to a real one.

You can't use $_SERVER["HTTP_REFERER"] because the client may choose not to send it, and this seems to be an essential bit of data.
In JavaScript you can only know the URL as it is displayed to the user (via document.location.href). If you want to know the filepath of the php script that handles that URL, just have PHP output the filepath somewhere on the page (in JS source) so JS can pick it up and pass it on.

Related

Make URL queries readable for php

I am trying to implement some php on an Apache 2+ Server which I do not have root access of. One script has to receive variables in a URL, but the API, that sends me the data, generates a URL-String with a # character in it.
The URL in Question would look like this:
http://website.name.com/script.php#foo=1&bar=2
Is there any way for the foo and bar variables to reach the script.php? I've read in other answers that everything after # doesn't get parsed by the server, so I tried to use an .htaccess file with a RewriteRule to replace the hashtag, but I was unable to create a working RegEx command.
No, the thing isn't that it isn't parsed by the server, the issue is that it's never being sent to the server. Everything after # is a local anchor, and is only available inside the current browser context (so Javascript would be able to read it, as it runs in the current browser context).
Since it's never sent to the server, you can't rewrite it or read it (since it doesn't exist) on the server side.
What you can do, is create a small bit of Javascript on the resulting page in script.php, and then submit that back to the server side - either through a redirect or through a fetch or xmlhttprequest.
To recreate the request as a GET request with the same parameters as given in the local anchor, you can use location.hash and remove the #:
location.href = 'realscript.php?' + location.hash.substring(1);
.. but I would consider parsing the hash yourself and then doing whatever is necessary in Javascript explicitly instead of redirecting like that.

How do I get this URL without considering the Apache settings?

HEllo I have this URL I need to get with PHP
http://www.domain.com/forum/#forum/General-discussions-0.htm
The problem is this is not a real URL, but this the mask created by the .htaccess.
I need to get the visible URL and not the real path of the file, because I need to compare it with some PHP variables I have.
In fact the real path will look like this:
http://domain.com/modules/boonex/forum/index.php
And in that way is totally useless for me.
How do I get the first URL as it is?
You can't get that from http://www.domain.com/forum/#forum/General-discussions-0.htm. Everything after the fragment (#) is not even send to the server, there is no way to retrieve it save for a delayed update with javascript. All you'll get it is http://www.domain.com/forum/ send to the server, and on the onload event of your document you can possibly load something in with javascript.
Look into the source code or it may not have real urls at all. The part is for ajax based navigation. It may mean that there are no real urls on that site and if there are then they should be extracted from <a href="someurl"> as they might masked using javascript.
With
file_get_contents();
for example. Neither user nor your server mind about .htaccess
It's server proccessing the request who have to direct you to correct address
however php does ignore everything after #, so in this case you have no chance to get it without real url
As #Wrikken said, there is no way to get url after # fragment

PHP - get everything after the #?

I've got a flash file (that I can't edit) that is making erroneous requests to a file ending in a #. So the link where the file is being accessed is /files/flash/, so the requests are being made for /files/flash/#
Is there any way for me to detect the # in the link so that I can have those pages not load?
The fragment is not sent to the server, and therefore cannot be accessed via PHP.
In case you are unsure:
scheme://host.tld/file/path/filename.ext?uri#fragment
The only way to access the fragment is through Javascript or another client-side script.

How can I store a full url that contains a hashmark within a variable?

I'm trying to store a url such as:
http://localhost/pro_print/index.php#page=home
in a variable, but I can't find a function that does that. I found plenty of solutions to store the index.php, but it doesn't include the hashmark and what follows it. Is there a PHP solution for this?
I did see that I can get the full url including hashmark with javaScript using document.write(document.url) or document.write(location.href) but how do I store that into my variable? Is there any way I can combine PHP with javaScript in some sort of solution like this?
<?php $url ="?><script>document.write(document.url)</script><?php "?>
The fragment identifier (the # and everything that appears after it) is handled entirely client side, and is not sent to the server when the URI is requested.
To make it available to PHP, you would have to:
Allow the page to load
Read the location with JavaScript
Send it to the server using an Ajax technique (e.g. XMLHttpRequest) or in a subsequent request
This won't make it available to the server at the time the original script runs, but nothing else can.
An alternative approach would be to duplicate the information in the fragment identifier somewhere else in the URI (e.g. the query string). This is used by this site when submitting an answer.

Is there a way to get the full contents of the address bar in php or htaccess?

Specifically. I am making an ajax app and trying to preserve the back button. My javascript is working properly and registering a new url in the address bar with an anchor-like hash in the url:
http://t2b.localhost/#/clients/
I can catch the url when the page loads with javascript and load the "clients" page, but I want to know if there is a way to read the entire url with php or with htaccess? Looking at normal variables, I seem to only be able to get the url up to the occurrence of the "#" (http://t2b.localhost/).
The browser don't send to the server the fragment (the text after the #) part of the url.
It is intended to be used locally by the client.
In firefox (and in explorer too) there is document.location.hash that contains the fragment part of the URL. If you use javascript you can read it and send his value into a common variable.
Please use any of the available javascript libraries to track the history state or browse by ajax requests. There are so many problems involved, such as certain browsers not notifying scripts when the hash part changes, or not adding a pseudo-'navigation' event to the browser's history list etc., that you'll end up recreating an expensive wheel that wouldn't work very well. I recommend YUI's History library, although it has problems on Google Chrome.
I'm pretty sure that you can't parse it strictly with PHP because the hash part is parsed only on the client-side ( Javascript ).
For history I'd recommend Ben Alman's BBQ plugin.
See: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
You could use javascript and set a cookie as the current URL then get it with PHP

Categories