0

I'm running a PHP built-in server with

php -S 127.0.0.1:80 index.php

I want to pass the entire URI string to a field called "url" in the $_GET array. When I enter http://localhost/thisIsAURLString, I want var_dump($_GET); to return array(1) { ["url"]=> string(16) "thisIsAURLString" } Is there some way to do this with the PHP built in server?

The web application is usually run in a production environment with nginx, and with a configuration file as shown below. This configuration passes the URL to a field "url" in the $_GET variable, but I want to do something similar with the PHP built-in server.

server {

    listen 5001 default_server;
    listen [::]:5001 default_server ipv6only=on;
    root [myRoot];
    index index.php index.html index.htm;
    server_name [myServerName];


    location /uploads {
                try_files $uri $uri/ =404;
        }

        location /assets {
                try_files $uri $uri/ =404;
        }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        rewrite ^/(.*)$ /index.php?url=$1 last;
    }

    location ~ .php$ {

        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php/php7.0-fpm-01.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

EDIT (some context) :

The context is that I'm a TA with many students. The web application in question is currently in a production environment with nginx and runs smoothly, but all of my ~100 students need to download and deploy the very same web application locally on their own computers. I can't alter the PHP code. The deployment should be as simple and smooth as possible, and if they can do this with some easily reproducible php command, that would be ideal.

Magnus
  • 589
  • 8
  • 26

3 Answers3

1

You could bootstrap your application with this script. Save this snippet to a file, and set it as your entry point in whatever web server software you're using. It will produce the results you're asking for.

<?php
   $root=__dir__;

   $uri=parse_url($_SERVER['REQUEST_URI'])['path'];
   $page=trim($uri,'/');  

   if (file_exists("$root/$page") && is_file("$root/$page")) {
       return false; // serve the requested resource as-is.
       exit;
   }

   $_GET['url']=$page;
   require_once 'index.php';
?>
enielsen
  • 26
  • 2
0

I'm not sure what you're asking but let me start with:

What "field" are you talking about?

Are you trying to print the url where?

What do you mean by "PHP built-in server"?

$_GET is a superglobal variable, array type, that's populated by PHP (a server-side scripting language). All you gotta do is call it (e.g. $_GET['link'] whereas link could be anything you'd want) or something similar (please check http://php.net/manual/en/reserved.variables.get.php). You can use it in any php file.

abr
  • 2,071
  • 22
  • 38
  • When I enter `http://localhost/thisIsAURLString`, I want `var_dump($_GET);` to return `array(1) { ["url"]=> string(16) "thisIsAURLString" }` – Magnus Feb 07 '18 at 17:51
  • By "PHP built-in server", I mean this feature: http://php.net/manual/en/features.commandline.webserver.php – Magnus Feb 07 '18 at 17:59
0

You may want to look at the global $_SERVER array. This contains the HTTP_HOST, QUERY_STRING, REQUEST_SCHEME and REQUEST_URI array keys. These can be used to assemble a full url. Try a var_dump($_SERVER); to see all the key => values.

Is there a particular reason you need to use the $_GET global array?

Hope this helps.

user9189147
  • 296
  • 1
  • 7
  • The reason is that I can't alter the underlying PHP code. It's written the way it's written, and I need to use server side logic to populate the $_GET variable. – Magnus Feb 07 '18 at 17:56
  • The answer is to use $_SERVER super global, specifically $_SERVER ["REQUEST_URI"] or $_SERVER['PATH_INFO'], which would return "/thisIsAURLString." Using the $_GET array introduces problems, and should be validated against a specific set, as it could be overwritten using the query string. For example, http://localhost/thisIsAURLString?url=. PHP/server populates the $_GET array from the query string, which could present a problem. Of course you could use $_SERVER in your PHP code to set $_GET['url'] = $_SERVER['REQUEST_URI'] but this probably is not a very good practice. – user9189147 Feb 07 '18 at 20:14