0

When i run this code on a local server it works fine, but if i run it on my domain it does not work, and i dont understand why.

I have tried to comment out sections on the page and it seems to crash when i define the 'siteTitle()' function. Here is the code up until the point it crashes:

index.php

<?php
    include('functions.php');
    varSet();
    if (include('pages/'.$pageID.'.php')) {
        // DO NOTHING
    } else {
        require('404.php');
    }
?>

<!DOCTYPE html>

<html lang="no">
    <head>
        <meta charset="utf-8" />
        <title><?php siteTitle('Obsidian.no', true, ' | '); ?></title>

functions.php

<?php

// Functions requierd by ~/index.php

    function isOnline() {
        if (preg_match('/::1$/', $_SERVER['SERVER_ADDR']) || $_SERVER['SERVER_ADDR']=='127.0.0.1') {
            return 0;
        } else if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_SERVER['SERVER_ADDR'])) {
            return 1;
        } else {
            return false;
        }
    }

    function varSet() {

    //  User-defined Globals goes here:

        // Set IP-variables

        $cIP = $_SERVER['HTTP_CLIENT_IP'];
        $xIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
        $rIP = $_SERVER['REMOTE_ADDR'];

        if (!empty($cIP)) {
            $usrIP = $cIP;
        } else if (!empty($xIP)) {
            $usrIP = $xIP;
        } else {
            $usrIP = $rIP;
        }

        // Set $root

        if (isOnline()) {
            $root = $_SERVER['DOCUMENT_ROOT'].'/design8';
        } else {
            $root = $_SERVER['DOCUMENT_ROOT'];
        }


        // Set $pageID

        if (isset($_GET['pageid'])&&!empty($_GET['pageid'])) {
            $pageID = $_GET['pageid'];
        } else {
            $pageID = 'home';
        }

        // Make Globals

        $GLOBALS['cIP'] = $cIP;
        $GLOBALS['xIP'] = $xIP;
        $GLOBALS['rIP'] = $rIP;
        $GLOBALS['usrIP'] = $usrIP;

        $GLOBALS['root'] = $root;

        $GLOBALS['pageID'] = $pageID;

    }

    function siteTitle($title, $displayPageName = true, $separator = ' | ') {
        global $root;
        global $pageID;
        if (get_required_files()['2']==$root.'/404.php') {
            echo($title.$separator.'Page not found (Error: 404)');
        } else {
            if ($displayPageName) {
                echo($title.$separator.ucwords($pageID));
            } else  {
                echo($title);
            }
        }
    }

I am aware that not all html and php tags are closed here, but thats because this is just the parts that run before the crash. If the 'siteTitle()' function is not commented out in functions.php, it crashes at 'require('functions.php')'. And if it is in comment it obviously crashes when the function is called in index.php.

However the site runs perfectly on MAMP localhost, but not from my domain hosted by one.com.

Thanks for any answers !

(404.php is irrelevant, as it does not contain anything)

The PHP version on the server is 5.6.3, and 5.6.2 on the localhost.

1
  • could try the older method $files=get_required_files(); then use $files[2] in the condition Commented Dec 15, 2014 at 2:39

2 Answers 2

6
get_required_files()['2']

This syntax is only supported in newer versions of PHP. 5.5 I believe.

If your server has an older version of PHP, it dies here.

To check for yourself, SSH into your server and run php -l functions.php - this will syntax-check your file and probably error out on that line.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, i will check the version. If it is too old, how do i work around this?
The PHP version on the server is 5.6.3, and 5.6.2 on the localhost.
@lask Enable error reporting or look in the logs for the error.
1

I managed to fix it by changing it to:

$files = get_required_files();
        if ($files[2]==$root.'/404.php')

as @Dagon suggested. Still dont understand since the version is newer on the server thoug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.