JS PHP

How to get url segment / address bar data from js and php

1. get url infomation via php

  <?php
    $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $uri_segments = explode('/', $uri_path);

    var_dump($uri_segments)
  ?>

2. get url information via jquery

  <script>
    var url = $(location).attr('href').split("/");
    var pname = url[url.length-1];
    console.log(url);
  </script>

Advertisement

Did you find this helpful? Share it!

Leave a Comment

Your email address will not be published. Required fields are marked