0

The given is a php code named function.php and is used in building an ecommerce website for my next project I tried to run it on xaamp but it showed me some errors so please have a look in it.

<?php
function strip_zeros_from_date($marked_string="") {
    //first remove the marked zeros
    $no_zeros = str_replace('*0','',$marked_string);
    $cleaned_string = str_replace('*0','',$no_zeros);
    return $cleaned_string;
}
function redirect_to($location = NULL) {
    if($location != NULL){
        header("Location: {$location}");
        exit;
    }
}
function redirect($location=Null){
    if($location!=Null){
        echo "<script>
                window.location='{$location}'
            </script>"; 
    }else{
        echo 'error location';
    }
     
}
function output_message($message="") {

    if(!empty($message)){
    return "<p class=\"message\">{$message}</p>";
    }else{
        return "";
    }
}
function date_toText($datetime=""){
    $nicetime = strtotime($datetime);
    return strftime("%B %d, %Y at %I:%M %p", $nicetime);    
                
}
//function __autoload($class_name)
// spl_autoload_register(function($class)
//spl_autoload_register('my_autoloader');
function __autoload($class_name) {
    $class_name = strtolower($class_name);
    $path = LIB_PATH.DS."{$class_name}.php";
    if(file_exists($path)){
        require_once($path);
    }else{
        die("The file {$class_name}.php could not be found.");
    }
                
}

function currentpage_public(){
    $this_page = $_SERVER['SCRIPT_NAME']; // will return /path/to/file.php
    $bits = explode('/',$this_page);
    $this_page = $bits[count($bits)-1]; // will return file.php, with parameters if case, like file.php?id=2
    $this_script = $bits[0]; // will return file.php, no parameters*/
     return $bits[2];
  
}

function currentpage_admin(){
    $this_page = $_SERVER['SCRIPT_NAME']; // will return /path/to/file.php
    $bits = explode('/',$this_page);
    $this_page = $bits[count($bits)-1]; // will return file.php, with parameters if case, like file.php?id=2
    $this_script = $bits[0]; // will return file.php, no parameters*/
     return $bits[4];
  
}
// echo "string " .currentpage_admin()."<br/>";

function curPageName() {
    return substr($_SERVER['REQUEST_URI'], 21, strrpos($_SERVER['REQUEST_URI'], '/')-24);
}

// echo "The current page name is ".curPageName();
 
function msgBox($msg=""){
    ?>
    <script type="text/javascript">
         alert(<?php echo $msg; ?>)
    </script>
    <?php
}
?>
  1. it's showing error in function __autoload($class_name) and I used spl_autoload_register(function($class_name) instead but it showed a different error with next line.

  2. the next line was function currentpage_public(){ it said

parse error: syntax error, unexpected token "function", expecting ")" in c:\xampp\htdocs\ecommerce\include\function.php on line 51

Now I am stuck on this point. Please guide me.

This is the code as I took: enter image description here

This is the error it showed after I changed the code to spl_autoload_register(function($class):

enter image description here

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • 1
    Looks like you forgot to close the spl_autoload_register function with a `);`. P.S. Please don't post images of code or errors - see [ask]. – ADyson Jan 05 '22 at 10:40
  • Why is this tagged with HTML and JavaScript? Please only add tags that are directly relevant to the issue at hand. – M. Eriksson Jan 05 '22 at 10:44
  • @ADyson when I use spl_autoload_register it gives me error on line 40 like this Parse error: syntax error, unexpected token "function", expecting ")" in C:\xampp\htdocs\ecommerce\include\function.php on line 51 line 51- function currentpage_public(){ all the function related are giving same error as above – sangram dighe Jan 05 '22 at 14:10
  • Yes you've already said that, and if you look, I've already told you how to fix it. Did you try it? – ADyson Jan 05 '22 at 14:35

0 Answers0