0

I am using the code below and it seems to be working fine but I can't seem to change the cookie name. It always defaults to PHPSESSID does anyone have any ideas where I am going wrong?

Thanks

//Enable composer based autoloading
require './vendor/autoload.php';

class_alias('Cartalyst\Sentry\Facades\Native\Sentry', 'Sentry');

$hasher = new Cartalyst\Sentry\Hashing\NativeHasher; // There are other hashers available, take your pick

$userProvider = new Cartalyst\Sentry\Users\Eloquent\Provider($hasher);

$groupProvider = new Cartalyst\Sentry\Groups\Eloquent\Provider;

$throttleProvider = new Cartalyst\Sentry\Throttling\Eloquent\Provider($userProvider);

$session = new Cartalyst\Sentry\Sessions\NativeSession;

// Note, all of the options below are, optional!
$options = array(
    'name'     => 'cartalyst_sentry', // Default "cartalyst_sentry"
    'time'     => null, // Default 300 seconds from now
    'domain'   => null, // Default ""
    'path'     => null, // Default "/"
    'secure'   => null, // Default "false"
    'httpOnly' => null, // Default "false"
);

$cookie = new Cartalyst\Sentry\Cookies\NativeCookie($options);

$sentry = new Sentry(
    $userProvider,
    $groupProvider,
    $throttleProvider,
    $session,
    $cookie
);

// Setup our database
$dsn      = 'mysql:dbname=database;host=localhost';
$user     = 'root';
$password = 'password';
$sentry::setupDatabaseResolver(new PDO($dsn, $user, $password));

try
{
    // Set login credentials
    $credentials = array(
        'email'    => 'email@gmail.com',
        'password' => 'password',
    );

    // Try to authenticate the user
    $user = $sentry::authenticate($credentials, false);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
    echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
    echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
    echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
    echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
    echo 'User is not activated.';
}

// The following is only required if throttle is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
    echo 'User is suspended.';
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
    echo 'User is banned.';
}

if ($sentry::check())
{
    echo 'User logged in.';
}
4

1 回答 1

0

Cartalyst\Sentry\Sessions\NativeSession 没有方法来更改会话的 cookie 名称(至少如果这是正确的类:https ://github.com/cartalyst/sentry/blob/master/src/Cartalyst/Sentry/会话/NativeSession.php)。

您可以session_name('cookie_name_here')在会话开始前通过调用自行设置。最简单的方法是在<?php. 试试它是否在那里工作。

文档

于 2013-08-15T08:38:06.400 回答