-1

I am just trying to make a simple PHP program that allows me to generate my page quickly.

I am completely new at PHP.. And I have no clue what I am doing.

/index.php

<?php
  include "/base/startup.php";
  echo "Test";
  startPage("Home");
 ?>

I'm getting a 500 server error with this.. Please tell me what I'm doing wrong. Thank you.

/base/startup.php

$HOME = "/";
$SCRIPT = <<<EOD

EOD;

$IMPORTS = array(
  "/scripts/script.js"
);

$STYLES = array(
  "/styles/style.css"
);

function prnt($string) {
  echo $string;
}

function map($func, $arr) {
  foreach($arr as $i) {
    call_user_func($func, $i);
  }
}

function linkScript($script) {
  prnt("<script src='$script'></script>");
}

function linkStyle($style)  {
  prnt("<link rel='stylesheet' href='$style'/>");
}

function startPage($title, $script="", $imports=array(), $styles=array()) {
  $pre_tags = array(
    "<html>",
    "<head>"
  );
  $post_tags = array(
    "</head>",
    "<body>"
  );
  map(prnt, $pre_tags);
  prnt("<title>$title</title>");
  map(linkScript, $IMPORTS);
  map(linkScript, $imports);
  map(linkStyle, $STYLES);
  map(linkStyle, $styles);
  map(prnt, $post_tags);
}

function genNav() {
  $nav_links = array(
    "Home"=>$HOME,
    "Walkthroughs"=>$HOME . "/walkthroughs/",
    "Dex"=>$HOME . "dex.php"
  );
  prnt("<div class='nav'>");
  foreach ($nav_links as $key => $value) {
    prnt("<a class='link' href='" . $value . "'/>" . $key . "</a>");
  }
}

function endPage() {
  $endTags = array(
    "</body>",
    "</html>"
  );
}

 ?>

This is the error:

Warning: include(/base/startup.php): failed to open stream: No such file or directory in /var/www/html/index.php on line 2

Warning: include(): Failed opening '/base/startup.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/index.php on line 2
Test
Fatal error: Uncaught Error: Call to undefined function startPage() in /var/www/html/index.php:4 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 4
mcchucklezz
  • 376
  • 1
  • 16

1 Answers1

3

Since you mentioned you are on a Linux machine, it looks like the issue is caused because of the / here. The / is considered the root directory of linux machine. So removing the / must most probably work:

<?php
  include "base/startup.php";  // Try removing the slash.
  echo "Test";
  startPage("Home");
?>

Since you haven't enabled the display of errors, the issue would be, there's no /base in your system and it would have thrown an error, like Fatal: Include file not found., which is not displayed because of your configuration, instead it would have shown Error 500 silently.


Update

Along with the above error, after seeing your code, the next one is you need to quote the function names. So replace the stuff with:

map("prnt", $pre_tags);
prnt("<title>$title</title>");
map("linkScript", $IMPORTS);
map("linkScript", $imports);
map("linkStyle", $STYLES);
map("linkStyle", $styles);
map("prnt", $post_tags);

The next error is, you haven't included the global variables correctly inside the function. You need to use:

global $IMPORTS, $STYLES;

Now your code works as expected.


And finally finishing the endPage() function:

function endPage() {
  $endTags = array(
    "</body>",
    "</html>"
  );
  foreach($endTags as $tag)
    echo $tag;
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252