简体   繁体   中英

Get ID from url php

This is my url

http://localhost:8888/App.php#?ID=1S

I needed the 1S as a variable for using it with a query.

If you want to parse URL as string:

$str = 'http://localhost:8888/App.php#?ID=1S';
$temp = explode( "?", $str );
$result = explode( "=", $temp['1'] );
echo $result['1'];

If you want to get it on server side:

Hash value is not sent to server side. So it impossible to get it on server side but you can use javascript to do some trick.


Using JavaScript/jQuery: (tags are not added though)

<script>

    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    hash = hashes[0].split('='); 
    alert( hash['1'] );

    // you can use jQuery.ajax() here to send this value to server side.

</script>
echo parse_url('http://localhost:8888/App.php#?ID=1S', PHP_URL_FRAGMENT);

OR

echo parse_url($_SERVER['QUERY_STRING'], PHP_URL_FRAGMENT);

If you need to parse it further:

$x = parse_url($_SERVER['QUERY_STRING'], PHP_URL_FRAGMENT);

parse_str($x, $arr);

echo $arr['ID']
$url = "http://localhost:8888/App.php#?ID=1S&another=3";
$a = parse_url($url);
parse_str($a["fragment"],$arr);
print_r($arr);

outputs:

Array (
    [?ID] => 1S
    [another] => 3
);

if you can live accessing the first parameter with "?ID"

I guess that the only way to do this is by an AJAX request, here is a simplified example:

the index page

<!doctype html>
<html>
<head>
    <title>Website</title>
    <script type="text/javascript">
        var url = document.location;
        url = url.toString();
        var getVal = url.split("#");
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('GET', 'App.php'+getVal[1], true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText);
            }
        }
        xmlhttp.send();
    </script>
</head>
<body>

</body>
</html>

the App.php page

<?php
    if (isset($_GET['url'])) echo 'url : ' . $_GET['url'];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM