Search code examples
phplightopenid

PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38


I'm trying to let people log in with steam, using this code:

<?php
 require './includes/lightopenid/openid.php'; 

$_STEAMAPI = "MyKey";

try {
    $openid = new LightOpenID('localhost');
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'http://steamcommunity.com/openid/?l=english';
            header('Location: ' . $openid->authUrl());
        } else {
            echo "<h2>Connect to Steam</h2>";
            echo "<form action='?login' method='post'>";
            echo "<input type='image' src='http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png'>";
            echo "</form>";
        }
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        if($openid->validate()) {
            echo "User is logged in.\n";

                $id = $openid->identity;
                $ptn = "/^http:\/\/steamcommunity\.com\/openid\/id/(7[0-9]{15,25}+)$/";
                preg_match($ptn, $id, $matches);      
                $url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$matches[1]";
                $json_object= file_get_contents($url);
                $json_decoded = json_decode($json_object);
                foreach ($json_decoded->response->players as $player) {

                }

        } else {
            echo "User is not logged in.\n";
        }
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}

?>

I get this error:

PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38

I can't find the issue! I know where the problem is, but can't find anything wrong with what I've done. What's the cause, and how do I fix it?


Solution

  • You forgot one "\" before "/)" on your pattern :

    $ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
    

    So php is thinking that "/(" is a modifier : http://php.net/manual/sr/reference.pcre.pattern.modifiers.php