Getting a # out of your url in PHP - 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.

Related

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.

document.referrer - limitations?

I am unable to get a lot of referral URLS using document.referrer. I'm not sure what is going on. I would appreciate it if anyone had any info on its limitations (like which browser does not support what) etc.
Is there something else i could use (in a different language perhaps) that covers more browsers etc?
I wouldn't put any faith in document.referrer in your Javascript code. The value is sent in client side request headers (Referer) and as such it can be spoofed and manipulated.
For more info see my answer to this question about the server side HTTP_REFERER server variable:
How reliable is HTTP_REFERER
Which browser are you looking in? If the referring website is sending the traffic via window.open('some link') instead of a regular <a> tag, then IE will not see a referrer. It thinks it's a new request at that point, similar to you simply going to a URL directly (in which case there is no referrer). Firefox and Chrome do not have the same issue.
This is NOT just a javascript limitation, HTTP_REFERRER will NOT work either in this specific scenario.
Just to make sure you're on the same page, you do know that if someone types a URL directly in their web browser, the document.referrer property is empty, right? That being said, you might be interested in a JavScript method to get all HTTP headers. If you prefer PHP (since you're using that tag), the standard $_SERVER variable will provide what information is available. Note that the information is only as reliable as the reporting web browser and server, as noted by Kev.
The document.referrer will be an empty string if:
You access the site directly, by entering the URL;
You access the site by clicking on a bookmark;
The source link contains rel="noreferrer";
The source is a local file;
Check out https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer

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

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