Get url variable with escape character [duplicate] - php

Assuming a URL of:
www.example.com/?val=1#part2
PHP can read the request variables val1 using the GET array.
Is the hash value part2 also readable? Or is this only upto the browser and JavaScript?

The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.
Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.

Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.
You can find the anchor name used within the URL via javascript using, as an example:
<script>alert(window.location.hash);</script>
The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
But I don't think PHP receives the fragment information because it's client-only.

It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.

The hash is never sent to the server, so no.

The answer is no.
The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.
The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.
Yes javascript can acces it, and then a simple ajax call will do the magic

What about:
Dynamically grab the #hash
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
//alert(txthash);
</script>
<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
To make it more fluent:
Full Example using just Javascript and PHP
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
function changehash(a,b){
window.location.hash = b; //add hash to url
//alert(b); //alert to test
location.reload(); //reload page to show the current hash
}
</script>
<?php $hash = "<script>document.writeln(txthash);</script>";?>
<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/>
<?php echo "This is the current hash: " . $hash; ?>

I think the hash-value is only used client-side, so you can't get it with php.
you could redirect it with javascript to php though.

Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.

<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>
This is should work

Yes you can:
Use this method to prevent errors:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
And of course in PHP, explode that puppy and get one of the values
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag

We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want

Another solution is to add a hidden input field to the php page:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
Using javascript/jQuery you can set the value of this field on the page load or responding to an event :
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
In php on the server side you can read this value using the $_POST collection:
$server_location_hash = $_POST['myHiddenLocationHash'];

Related

Find targeted #id from parent includer with php [duplicate]

Assuming a URL of:
www.example.com/?val=1#part2
PHP can read the request variables val1 using the GET array.
Is the hash value part2 also readable? Or is this only upto the browser and JavaScript?
The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.
Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.
Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.
You can find the anchor name used within the URL via javascript using, as an example:
<script>alert(window.location.hash);</script>
The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
But I don't think PHP receives the fragment information because it's client-only.
It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.
The hash is never sent to the server, so no.
The answer is no.
The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.
The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.
Yes javascript can acces it, and then a simple ajax call will do the magic
What about:
Dynamically grab the #hash
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
//alert(txthash);
</script>
<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
To make it more fluent:
Full Example using just Javascript and PHP
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
function changehash(a,b){
window.location.hash = b; //add hash to url
//alert(b); //alert to test
location.reload(); //reload page to show the current hash
}
</script>
<?php $hash = "<script>document.writeln(txthash);</script>";?>
<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/>
<?php echo "This is the current hash: " . $hash; ?>
I think the hash-value is only used client-side, so you can't get it with php.
you could redirect it with javascript to php though.
Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.
<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>
This is should work
Yes you can:
Use this method to prevent errors:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
And of course in PHP, explode that puppy and get one of the values
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want
Another solution is to add a hidden input field to the php page:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
Using javascript/jQuery you can set the value of this field on the page load or responding to an event :
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
In php on the server side you can read this value using the $_POST collection:
$server_location_hash = $_POST['myHiddenLocationHash'];

Why i am not get exact passing text using $_GET when that text contains #? [duplicate]

Assuming a URL of:
www.example.com/?val=1#part2
PHP can read the request variables val1 using the GET array.
Is the hash value part2 also readable? Or is this only upto the browser and JavaScript?
The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.
Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.
Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.
You can find the anchor name used within the URL via javascript using, as an example:
<script>alert(window.location.hash);</script>
The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
But I don't think PHP receives the fragment information because it's client-only.
It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.
The hash is never sent to the server, so no.
The answer is no.
The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.
The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.
Yes javascript can acces it, and then a simple ajax call will do the magic
What about:
Dynamically grab the #hash
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
//alert(txthash);
</script>
<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
To make it more fluent:
Full Example using just Javascript and PHP
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
function changehash(a,b){
window.location.hash = b; //add hash to url
//alert(b); //alert to test
location.reload(); //reload page to show the current hash
}
</script>
<?php $hash = "<script>document.writeln(txthash);</script>";?>
<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/>
<?php echo "This is the current hash: " . $hash; ?>
I think the hash-value is only used client-side, so you can't get it with php.
you could redirect it with javascript to php though.
Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.
<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>
This is should work
Yes you can:
Use this method to prevent errors:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
And of course in PHP, explode that puppy and get one of the values
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want
Another solution is to add a hidden input field to the php page:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
Using javascript/jQuery you can set the value of this field on the page load or responding to an event :
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
In php on the server side you can read this value using the $_POST collection:
$server_location_hash = $_POST['myHiddenLocationHash'];

Read the URL part after # hash with PHP [duplicate]

Assuming a URL of:
www.example.com/?val=1#part2
PHP can read the request variables val1 using the GET array.
Is the hash value part2 also readable? Or is this only upto the browser and JavaScript?
The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.
Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.
Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.
You can find the anchor name used within the URL via javascript using, as an example:
<script>alert(window.location.hash);</script>
The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
But I don't think PHP receives the fragment information because it's client-only.
It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.
The hash is never sent to the server, so no.
The answer is no.
The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.
The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.
Yes javascript can acces it, and then a simple ajax call will do the magic
What about:
Dynamically grab the #hash
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
//alert(txthash);
</script>
<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
To make it more fluent:
Full Example using just Javascript and PHP
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
function changehash(a,b){
window.location.hash = b; //add hash to url
//alert(b); //alert to test
location.reload(); //reload page to show the current hash
}
</script>
<?php $hash = "<script>document.writeln(txthash);</script>";?>
<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/>
<?php echo "This is the current hash: " . $hash; ?>
I think the hash-value is only used client-side, so you can't get it with php.
you could redirect it with javascript to php though.
Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.
<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>
This is should work
Yes you can:
Use this method to prevent errors:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
And of course in PHP, explode that puppy and get one of the values
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want
Another solution is to add a hidden input field to the php page:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
Using javascript/jQuery you can set the value of this field on the page load or responding to an event :
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
In php on the server side you can read this value using the $_POST collection:
$server_location_hash = $_POST['myHiddenLocationHash'];

Query string as parameter in htaccess [duplicate]

Assuming a URL of:
www.example.com/?val=1#part2
PHP can read the request variables val1 using the GET array.
Is the hash value part2 also readable? Or is this only upto the browser and JavaScript?
The main problem is that the browser won't even send a request with a fragment part. The fragment part is resolved right there in the browser. So it's reachable through JavaScript.
Anyway, you could parse a URL into bits, including the fragment part, using parse_url(), but it's obviously not your case.
Simple test, accessing http://localhost:8000/hello?foo=bar#this-is-not-sent-to-server
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [02/Jun/2009 12:48:47] code 404, message File not found
localhost - - [02/Jun/2009 12:48:47] "GET /hello?foo=bar HTTP/1.1" 404 -
The server receives the request without the #appendage - anything after the hash tag is simply an anchor lookup on the client.
You can find the anchor name used within the URL via javascript using, as an example:
<script>alert(window.location.hash);</script>
The parse_url() function in PHP can work if you already have the needed URL string including the fragment (http://codepad.org/BDqjtXix):
<?
echo parse_url("http://foo?bar#fizzbuzz",PHP_URL_FRAGMENT);
?>
Output: fizzbuzz
But I don't think PHP receives the fragment information because it's client-only.
It is retrievable from Javascript - as window.location.hash. From there you could send it to the server with Ajax for example, or encode it and put it into URLs which can then be passed through to the server-side.
The hash is never sent to the server, so no.
The answer is no.
The main purpose of the hash is to scroll to a certain part of the page where you have defined a bookmark. e.g. Scroll to this Part when page loads.
The browse will scroll such that this line is the first visible content in the page, depending on how much content follows below the line.
Yes javascript can acces it, and then a simple ajax call will do the magic
What about:
Dynamically grab the #hash
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
//alert(txthash);
</script>
<?php
$hash = "<script>document.writeln(txthash);</script>";
echo $hash;
?>
To make it more fluent:
Full Example using just Javascript and PHP
<script>
var urlhash = window.location.hash, //get the hash from url
txthash = urlhash.replace("#", ""); //remove the #
function changehash(a,b){
window.location.hash = b; //add hash to url
//alert(b); //alert to test
location.reload(); //reload page to show the current hash
}
</script>
<?php $hash = "<script>document.writeln(txthash);</script>";?>
<a onclick="changehash(this,'#hash1')" style="text-decoration: underline;cursor: pointer;" >Change to #hash1</a><br/>
<a onclick="changehash(this,'#hash2')" style="text-decoration: underline;cursor: pointer;">Change to #hash2</a><br/>
<?php echo "This is the current hash: " . $hash; ?>
I think the hash-value is only used client-side, so you can't get it with php.
you could redirect it with javascript to php though.
Th part of an URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.
<?php
$url=parse_url("http://domain.com/site/gallery/1?user=12#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
?>
This is should work
Yes you can:
Use this method to prevent errors:
<script>
query=location.hash;
document.cookie= 'anchor'+query;
</script>
And of course in PHP, explode that puppy and get one of the values
$split = explode('/', $_COOKIE['anchor']);
print_r($split[1]); //to test it, use print_r. this line will print the value after the anchortag
We can do it with another approach too, Like first of all get the hash value from js and call the ajax using that parameter and can do whatever we want
Another solution is to add a hidden input field to the php page:
<input type="hidden" id="myHiddenLocationHash" name="myHiddenLocationHash" value="">
Using javascript/jQuery you can set the value of this field on the page load or responding to an event :
$('#myHiddenLocationHash').val(document.location.hash.replace('#',''));
In php on the server side you can read this value using the $_POST collection:
$server_location_hash = $_POST['myHiddenLocationHash'];

PHP isset($_GET) not triggered

live at http://vivavidadesign.com/includes/content.php
I have the PHP code:
<?php
if (isset($_GET["#photo"])){
print ("HELLOOOOOOO");
} else {
print ("Nothing Set");
}
?>
I am using this file in to return files in conjunction with AJAX.
Previously going to http://vivavidadesign.com/includes/content.php#photo would trigger (isset($_GET["#photo"])). I can trigger it with ?photo, but I want to know how to trigger using a hash i.e. #photo
The browser is never sending hash to the server, when requesting data.
You have to send it manually, ie. using jquery:
var hash = window.location.hash;
$.ajax({ url: 'content.php?photo=' + hash});
PHP does not grab a hash / anchor.
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
You would have to parse it using Javascript and possibly send via ajax to your php script.
#xxx at the end of URL is not argument to the page but anchor in the page. So PHP doesn't put it in GET variable.
Either use
[code]
http://vivavidadesign.com/includes/content.php?%23photo
[/code]
if you really need the name of variable to be #photo or you can pass variable in the classic wat like this
[code]
http://vivavidadesign.com/includes/content.php?photo=something
[/code]
<?php
if(isset($_GET["photo"])) {
echo $_GET["photo"];
} else {
echo "Nothing Set";
}
?>
And call it:
index.php?photo=file.jpg
Btw. do not use print if it is not needed, use echo instead.
How to get the value after the hash in "somepage.php#name"?
A get is included in the URL after a questionmark, eg.
http://www.example.com/?photo=1
An anchor is included in the URL after a hashtag, eg.
http://www.example.com/#photo
An anchor is never sent to the server from the browser, but you might be able to find it using javascript and through javascript pass it on to the server.
If you want your variable to be a get you need to change the $_GET['#photo'] into $_GET['photo'] in your code and #photo into ?photo=something in your URL.
You have to choose one php or javascript to get value :
PHP
$ar = explode("#",$_SERVER['REQUEST_URI']);
print_r($ar[1]);
Demo : https://eval.in/85871
2 JS:
var hash = window.location.hash;
document.location.href = '/content.php?photo='+hash;
This is similar to this other question.
You do this not with $_GET, but with parse_url().
The part you are looking for is the fragment
Array
(
[path] => includes/content.php
[fragment] => photo
)
Edit to clarify:
If this URL is coming from the client, you're misusing/misunderstanding how the hashtag functions in a URL: http://en.wikipedia.org/wiki/Fragment_identifier . You must parse it client side via javascript.

Categories