How to get values after .php

I am wondering how I can grab all the values from the URL after .php?

Example: http://localhost/api/test

API = api.php, just .htaccess.

And I am wondering how I can grab only "test" from the URL, or al the values if more, /test/test1/test2

I've tried $_SERVER['PATH_INFO'] but it doesn't work for me, error output saying that $_SERVER['PATH_INFO'] is undefined. Is there any workaround?

Try this

Using parse_url() you can retrieve url components

$url = "http://localhost/api/test";
$myurl = parse_url($url);
echo  $myurl['path'];

How to get values after .php, Try this. Using parse_url() you can retrieve url components $url = "http://localhost/‚Äčapi/test"; $myurl = parse_url($url); echo $myurl['path'];. The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods. Try out following example by putting the source code in test.php script.

You can find all you are looking for in the $_SERVER variable (php.net) Just print_r the value of $_SERVER and see what information is there... You will see quite easy what you will need. You can then use string functions to isolate the part you need in the URL or query string to fetch whatever you need.

Also...

If a .htaccess is used with rewrites, they will probably be sent in the $_GET variables. To be sure, you need to post some of your .htaccess where the rewrites are being done.

How to get the last char of a string in PHP?, How can I get the last letter of a string in PHP? Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information. Note: If the return is omitted the value NULL will be returned.

Simple steps for you

Step One: write in .htaccess

RewriteEngine on
RewriteRule  ^(.+)/?$ test.php?url=$1

Step Two: test.php file with following code

<?php

   $url = $_GET['url'];
   echo '<pre>';
       print_r($url);
   echo '</pre>';
?>

NOTE: you can call explode function on $url variable

rewrite module must be on in Apache server.

Note i think this will solve your problem

script is self explanatory but if confusion then tell me .

<?php
  $url = $_GET['url'];

  echo '<pre>';
      print_r($url);
  echo '</pre>';

  //echo '<pre>';
      ///print_r(explode('/',$url,4));
  //echo '</pre>';

  list($file,$controller,$method,$params) = explode('/',$url,4);


  echo "<hr/>file:" . $file;
  echo "<hr/>controller:" . $controller;
  echo "<hr/>method:" . $method;

  echo "<hr/>params:" ;
  echo '<pre>';
      print_r($params);
  echo '</pre>';

  ?>

Output 2

How to return multiple values from function in PHP?, and not part of the CRLF (Carriage Return Line Feed). The GET method cannot be used to send binary data like images, mp3 or pdf files to the server. When you submit a form through the GET method, PHP creates a $_GET associative array in this format, $_GET['name as key'] to enable you to retrieve the form data. The GET method is suitable to send non-sensitive content/information to the server.

PHP - Functions, How do I get the first letter of a string in PHP? Get Value of Select Option and Radio Button; Get Values of Multiple Checked Checkboxes; Redirect To URL After Form Submission; Upload Images Using PHP and jQuery via form; POST data using fsockopen() Function; Form; Multi Page Form; Validation; Form Validation; Contact Form Validation; Sanitize and Validate Input Fields; Session; Login Form with Sessions; Miscellaneous

PHP String Exercise: Get the characters after the last '/' in an url , PHP String Exercises: Get the characters after the last '/' in an url cookies must be created with a function (and not be assigning a value). Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag.. For Example, change the method in your form and then echo out the value by the name of the input:

substr - Manual, echo substr($description,0,strpos($description,' ',30)); //strpos to find ' ' after 30 A negative range value means to count from the end of the string instead of the  I tested this with script.php?a, and it returned: a is an empty string a is set So note that a parameter with no value associated with, even without an equals sign, is considered to be an empty string (""), isset() returns true for it, and it is considered empty, but not false or null. Seems obvious after the first test, but I just had to make

Comments
  • $_GET if it's set this way ... $_SERVER['REQUEST_URI'] if not
  • you can refer here
  • But i am not doing api.php?a=test1&b=test2 SO $_GET is useless. Im trying to get the path after localhost/api
  • Then your best chance is to get $url = $_SERVER['REQUEST_URI']; and then $url = epxlode(... then use $url[4] etc.. or you could replace the 'localhost/api' with '' first.
  • @MikeRunac if you have ? in url ... $_GET shouldn't be useless ...
  • @mike-runac support back if it really helped then mark as answer and thumb.
  • Passing everything through as an URL parameter is a bit old skool - you'd normally redirect everything that's not an existing file/directory/(or symlink possibly) to the routing script that checks $_SERVER['REQUEST_URI'] to work out what action to perform.
  • but old school tricks always problem solver. :) BTW you way is also right