0

I am new to programming, and I decided to try PHP. I am trying to set the variable $_SERVER["SERVER_NAME"]. Unfortunately, I'm getting some errors.

The code is copied from this Youtube tutorial, so it is supposed to be correct.

$server = [
         'Host Server Name' => $_SERVER['SERVER_NAME'],
         'Host Header' => $_SERVER['HTTP_HOST'],
         'Server Software' => $_SERVER['SERVER_SOFTWARE'],
         'Document Root' => $_SERVER['DOCUMENT_ROOT'],
         'Current Page' => $_SERVER['PHP_SELF'],
         'Script Name' => $_SERVER['SCRIPT_NAME'],
         'Absolute Path' => $_SERVER['SCRIPT_FILENAME']
];


//print_r($server);

// Create Client Array
$client = [
    'Client System info' => $_SERVER['HTTP_USER_AGENT'],
    'Client IP' => $_SERVER['REMOTE_ADDR'],
    'Remote Port' => $_SERVER['REMOTE_PORT']
];

The error:

Notice: Undefined variable:** server in C:\xampp\htdocs\phpsandbox\website2\server-info.php on line 7

Fatal error: Uncaught Error: Unsupported operand types in C:\xampp\htdocs\phpsandbox\website2\server-info.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\phpsandbox\website2\server-info.php on line 7

Thanks to anyone for the support!

Kim
  • 17
  • 5
  • 3
    Your question title does not match the error message – Patrick Q May 29 '19 at 17:24
  • 3
    Can you show your full code? It's not balking at `$_SERVER['SERVER_NAME']`, it's balking because `$server` is not defined when you try to use it on line 7, which usually happens when it's defined inside an if block – aynber May 29 '19 at 17:24
  • sorry for the title, I actually do not know the problem. I edited the full code, which is copied by the teacher to be honest. – Kim May 29 '19 at 17:29
  • Try setting the server name as described in https://stackoverflow.com/a/2297421/5779083, if @aynber s solution doesn't work. – Tom May 29 '19 at 17:30
  • Please show the first 10 lines of `server-info.php` – aynber May 29 '19 at 17:34
  • @aynber this is the full code as copied from the tutorial – Kim May 29 '19 at 17:36
  • 2
    Line 7 does not reference `$server`, nor have an operand (math), so the error is not in this code. – aynber May 29 '19 at 17:37
  • @aynber I guess Tom solution is correct, even if I have no idea how to apply. Thanks a lot for the support – Kim May 29 '19 at 17:40

0 Answers0