Parse error: syntax error, unexpected 'public' (T

I have This class for PHP redirect:

<?PHP
public static function redirect($uri,$code=302)
{
    // Specific URL
    $location = null;
    if (substr($uri,0,4)=='http') {
        $location = $uri;
    } else {
        $location = self::base(true);
        // Special Trick, // starts at webserver root / starts at app root
        if (substr($uri,0,2) == '//') {
            $location .= '/' . ltrim($uri,'/');
        } elseif (substr($uri,0,1) == '/') {
            $location .= '/' . ltrim($uri,'/');
        }
    }

    // $sn = $_SERVER['SCRIPT_NAME'];
    // $cp = dirname($sn);
    // $schema = $_SERVER['SERVER_PORT']=='443'?'https':'http';
    // $host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
    // if (substr($to,0,1)=='/') $location = "$schema://$host$to";
    // elseif (substr($to,0,1)=='.') // Relative Path
    // {
    //   $location = "$schema://$host/";
    //   $pu = parse_url($to);
    //   $cd = dirname($_SERVER['SCRIPT_FILENAME']).'/';
    //   $np = realpath($cd.$pu['path']);
    //   $np = str_replace($_SERVER['DOCUMENT_ROOT'],'',$np);
    //   $location.= $np;
    //   if ((isset($pu['query'])) && (strlen($pu['query'])>0)) $location.= '?'.$pu['query'];
    // }
    // }

    $hs = headers_sent();
    if ($hs === false) {
        switch ($code) {
        case 301:
            // Convert to GET
            header("301 Moved Permanently HTTP/1.1",true,$code);
            break;
        case 302:
            // Conform re-POST
            header("302 Found HTTP/1.1",true,$code);
            break;
        case 303:
            // dont cache, always use GET
            header("303 See Other HTTP/1.1",true,$code);
            break;
        case 304:
            // use cache
            header("304 Not Modified HTTP/1.1",true,$code);
            break;
        case 305:
            header("305 Use Proxy HTTP/1.1",true,$code);
            break;
        case 306:
            header("306 Not Used HTTP/1.1",true,$code);
            break;
        case 307:
            header("307 Temporary Redirect HTTP/1.1",true,$code);
            break;
        }
        header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        header("Location: $location");
    }
    // Show the HTML?
    if (($hs==true) || ($code==302) || ($code==303)) {
        // todo: draw some javascript to redirect
        $cover_div_style = 'background-color: #ccc; height: 100%; left: 0px; position: absolute; top: 0px; width: 100%;';
        echo "<div style='$cover_div_style'>n";
        $link_div_style = 'background-color: #fff; border: 2px solid #f00; left: 0px; margin: 5px; padding: 3px; ';
        $link_div_style.= 'position: absolute; text-align: center; top: 0px; width: 95%; z-index: 99;';
        echo "<div style='$link_div_style'>n";
        echo "<p>Please See: <a href='$uri'>".htmlspecialchars($location)."</a></p>n";
        echo "</div>n</div>n";
    }
    exit(0);
}
?>

I redirect Using:

redirect("http://www.foo.bar/page.htm",303)

but in page I see this error: Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:xampphtdocsuserclassmain.class.php on line 2

how do can I fix this error?!


Remove public static .

Those Keywords are for function definitions inside a class.

Alternative: define a class around your function, this will take care of other class related references as well:

class MyClass {
// your function here
}

then you can call your function like this:

MyClass::redirect();

For remove error define a class

class Foo
{
    public static function redirect($uri,$code=302) {
       // your function body
    }
}

and call function like:-

Foo::redirect("http://www.foo.bar/page.htm",303);

or remove public static and include file where you calling function

链接地址: http://www.djcxy.com/p/69486.html

上一篇: 解析错误:语法错误,文件意外结束

下一篇: 解析错误:语法错误,意想不到的'public'(T