How can i get the page.html#PARAMETERS through PHP? - php

How can I get the value of the URL hash (eg "PARAMETERS" in url "page.html#PARAMETERS") on the server in PHP?

You can't, at least not directly. The fragment identifier is never sent to the server.
You could extract it in JS using location.hash and then trigger another HTTP request to the server that includes it as data (e.g. in the query string).

You can't, unless you got something client-side to explicitly transfer it to you. The fragment part is only handled by the browser.

Related

Getting a # out of your url in PHP

I know that $_GET["whatever"] gets ?whatever=*
Is there a way to get the # variable out of the url:
www.*.com#imsomething
? Can't find any results on google.
The part after # is a client-side part of the URL, it refers to an anchor within the HTML. Therefore, you can only retrieve everything before that.
Clients are not supposed to send URI-fragments to servers when they retrieve a document, and without help from a local application fragments do not participate in HTTP redirections.
~ Wikipedia: http://en.wikipedia.org/wiki/Fragment_identifier
You can use this Javascript code to get it's content client-side:
var hash = window.location.hash;
After grabbing it, you can of course send it to your server side code using jQuery's $.ajax or something similar.
No, there is not, since that is never sent to server side. That is something that is only visible on the browser.
You can read it with javascript and then submit to server side if you really want to, but normally it doesn't get sent. With javascript, you can read window.location.hash to get the value.
No, that is not possible with a server side language... You need JavaScript to read that.

How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie?

How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie ?
(from PHP to JavaScript: var javaScriptVar = "<?php echo $someVar; ?>"; )
So is there nothing like that?
thanks
You can do it like in your example, but your javascript must either be inside a script tag which is in a PHP file, or in an external 'js' file that you save with a php extension.
Or, you can set your server up so that all files with a 'js' extension also get parsed by PHP, but that's probably more than overkill.
However, since the PHP will only be executed once before the file is returned to the client, there's no way to make that dynamic. If your objective is to do something simple with data that resides on the server (and more than likely will not change between page accesses) it won't be too horrible to accomplish what you want to do that way.
There is no way to get a Javascript value from PHP, because the response line goes from server to browser, never the other way around. AJAX is your best bet in this case.
There is nothing like that. The data needs to be transfered to the server, either by a form variable or a url variable. Either with an AJAX request or a normal request. I never thought about doing it with a cookie, but I guess that is an option too.
No.
PHP is a server sided language, javascript is a client sided language.
In order for PHP to receive information, you must talk to the server. The only way to do so is to submit a form, or use AJAX.
Short answer is you can't. There are two main ways to send content to the server from the client, which are cookies and submitted data (either posted or through the querystring). If you were so inclined you could add it to a request header but that is plain obscure and would require an AJAX request.

What's the best way to access a URI variable after a hashtag from php?

I have Javascript updating my URI as below:
/index.php?page=list#page=news
But I would like to make page=news accessible somehow from my server so that when the URI is copied and pasted, it would go to the news page. What is the best way to do this?
I tried $_SERVER['REQUEST_URI'] but everything stored in $_SERVER is before the hash tag.
You can't. That data, called the fragment, is reserved for client side processing and thus is never sent to the server.
The only way to utilize the fragment is to have Javascript intervene at some point. This probably means checking for a hash-tag on the page onload, and then displaying the proper data.
You could make your entire page loaded via Javascript. While it would kill compatability for anyone who turned off Javascript, it would ensure that the hash tag eventually gets sent to PHP
Basically, it would look something like this:
PHP Sends Page
Javascript reads the hastag
Make a URL with a hashtag parameter (loader.php?page=list&page=news)
(Note that in the above, page=list wil be overriden by page=news, so $_GET['page'] will be news.
AJAX call to PHP
Load the content into a div.
(And this question is very much a duplicate question)

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.

GET PHP but using # instead of ?=

When using the $_GET[] in php, it searches for a variable e.g ?id=value
Is there a way to call the #value instead?
No, because the hash part of the url is client-side only and is not sent to the server.
When you enter an URL such as http://server.com/dir/file.php?a=1#something in your browser's URL textbox, the browser opens a connection to the server.com and then issues a HTTP command GET /dir/file.php?a=1 HTTP/1.1. This is the only data sent to the server.
Hence, the server never gets the #something part, and this means there is no script on server side you could write to read that value.
Similar question explained here: How to get Url Hash (#) from server side
I've been able to work around it by getting the fragment via javascript and sending an ajax request with the fragment as the $_GET contents.
Without knowing your whole case, I may be off track, but there is the possibility of sending the #something to the server via a simple GET type of xmlhttprequest.
Yeah there is a way. I think what you want to do is:
$arValues = array_values($_GET);
// whatever else you want to do with the values

Categories