本文整理匯總了PHP中session::add_new_session方法的典型用法代碼示例。如果您正苦於以下問題:PHP session::add_new_session方法的具體用法?PHP session::add_new_session怎麽用?PHP session::add_new_session使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類session
的用法示例。
在下文中一共展示了session::add_new_session方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: startNewSession
public static function startNewSession($username, $password, $gcm_id)
{
$success = true;
//test if the username and password are correct
if (user::isLogin($username, $password)) {
//retrieve user info
$user_info = user::getUserByUsername($username);
$e = user::setGCM($user_info['id'], $gcm_id);
$success = $success && $e;
//check if user has existing session:
if (session::does_user_have_session($user_info['id'])) {
//remove the session
$session_info = session::get_last_session_for_user_id($user_info['id']);
session::delete_session_by_id($session_info['id']);
}
//generate a unique hash
$newHash = md5(random::generateString(10));
while (!session::is_unique_hash($newHash)) {
$newHash = md5(random::generateString(10));
}
//create a session
$res = session::add_new_session($user_info['id'], $newHash, "0");
$success = $success && $res;
if (!$success) {
Execute::$lastErrorMessage = "failed to add new changes to database";
Report::error(__METHOD__ . "," . __LINE__, "failed to new cahnges to database");
}
return $success;
} else {
Execute::$lastErrorMessage = "trying to login with an incorrect username or password";
Report::warning(__METHOD__ . "," . __LINE__, "trying to login with an incorrect username or password");
return false;
//trying to log in with an incorrect username or password
}
}
示例2: test
public function test()
{
$username = "howdy";
$password = "123456";
$email = "gg@gmail.com";
$username2 = "bla2";
$password2 = "pass2";
$email2 = "gr@gmmail.com";
$username3 = "gue";
$password3 = "pass3";
$email3 = "eee@gmail.com";
user::create_new_user($username, $password, $email);
$this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
user::create_new_user($username2, $password2, $email2);
$this->assertEquals(2, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
user::create_new_user($username3, $password3, $email3);
$this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
$user1ID = user::getUserByUsername($username)['id'];
$user2ID = user::getUserByUsername($username2)['id'];
$user3ID = user::getUserByUsername($username3)['id'];
$this->assertTrue(safe_input::is_valid_session_hash(md5("\$3dfsd43^^%")), "safe_input::is_valid_session_hash()");
$this->assertFalse(safe_input::is_valid_session_hash("x = 2 "), "safe_input::is_valid_session_hash()");
//get_last_session_for_user_id($user_id)
//add_new_session($user_id,$hash,$encryption_key)
$this->assertFalse(session::add_new_session("hh", md5("bla"), md5("bla2")), "[add_new_session()]");
$this->assertTrue(session::add_new_session($user1ID, md5("bla"), md5("bla2")), "failed to add new session[add_new_session()]");
$s1 = session::$last_inserted_id;
$this->assertFalse(session::is_unique_hash(md5("bla")), "[session::is_unique_hash()]");
$this->assertTrue(session::is_unique_hash(md5("bddla")), "[session::is_unique_hash()]");
//$this->assertNull(session::add_new_session($user2ID,md5("bla"),md5("mmm")),"Hash has to be unique! [add_new_session()]") ;
$this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]");
$this->assertTrue(session::add_new_session($user1ID, md5("hash2sss"), md5("blssssa2")), "failed to add new session[add_new_session()]");
$s2 = session::$last_inserted_id;
$s_info = session::get_last_session_for_user_id($user1ID);
$this->assertEquals($s_info['encryptionKey'], md5("blssssa2"), "[get_last_session_for_user_id()]");
$this->assertFalse(session::is_unique_hash($s_info['hash']), "[is_unique_hash()]");
$this->assertTrue(session::is_unique_hash(md5("asdfasefds")), "[is_unique_hash()]");
$this->assertTrue(session::does_user_have_session($user2ID), "[does_user_have_session()]");
$this->assertFalse(session::does_user_have_session($user3ID), "[does_user_have_session()]");
//delete_all_sessions_for_user_id($user_id)
$this->assertFalse(session::delete_all_sessions_for_user_id("sadsadsad"), "[delete_all_sessions_for_user_id()]");
$this->assertTrue(session::delete_all_sessions_for_user_id($user2ID), "[delete_all_sessions_for_user_id()]");
$this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]");
$this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]");
//session::delete_session_by_id($session_id)
$s_info = session::get_last_session_for_user_id($user1ID);
$this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]");
$s2_info = session::get_last_session_for_user_id($user2ID);
$this->assertTrue(session::delete_session_by_id($s2_info['id']), "[delete_session_by_id()]");
$this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]");
$this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]");
//session::delete_session_by_hash($hash)
$s_info = session::get_last_session_for_user_id($user1ID);
$this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]");
$s2_info = session::get_last_session_for_user_id($user2ID);
$this->assertTrue(session::delete_session_by_hash($s2_info['hash']), "[delete_session_by_id()]");
$this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]");
$this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]");
//session::get_session_by_hash($hash)
$s_infos = session::get_session_by_hash($s_info['hash']);
$this->assertEquals($s_info['id'], $s_infos['id'], "session::get_session_by_hash()");
}
示例3: testNewGameRequest
public function testNewGameRequest()
{
//startNewGame()
$username = "troy";
$password = "345667";
$email = "troy1970@gmail.com";
$username2 = "sandy";
$password2 = "booha";
$email2 = "sandy1111@gmail.com";
user::create_new_user($username, $password, $email);
user::create_new_user($username2, $password2, $email2);
$user_info = user::getUserByUsername($username);
$user_info2 = user::getUserByUsername($username2);
$session = md5("dfgfds4543");
$session2 = md5("rtyertyerty");
$this->assertTrue(session::add_new_session($user_info['id'], $session, "0"));
$this->assertTrue(session::add_new_session($user_info2['id'], $session2, "0"));
$xmlFile = file_get_contents("./files/newPendingGameRequest.xml");
$p = simplexml_load_string($xmlFile);
$p->body->session = $session;
$p->body->size = 6;
$req = $p->asXML();
$obj = new XmlParseRequest();
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$this->assertEquals("5", $pr->body->id, "[new pending game]");
$this->assertEquals($session, $pr->body->session, "[new pending game]");
$games = pending_game::get_all_pending_games_for_user_id($user_info['id']);
$this->assertEquals(1, count($games), "[new pending game]");
$this->assertEquals(6, $games[0]['size'], "[new pending game]");
//send an invalid session hash
$p->body->session = md5("invalid_session");
$req = $p->asXML();
$obj = new XmlParseRequest();
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("failed", $pr->body->status, "[new pending game]");
$this->assertEquals("1", $pr->body->error_code, "error code is not correct (invalid session hash passed)[new pending game]");
// add a second pending game fo the same user and check that the system dodn't match the games since they belong to the same user!
$p->body->session = $session;
$req = $p->asXML();
$obj = new XmlParseRequest();
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$p->body->session = $session;
$req = $p->asXML();
$obj = new XmlParseRequest();
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$this->AssertEquals(0, game::getNumberOfGames(), "one pending games with the same size added[new pending game]");
$p->body->session = $session2;
$req = $p->asXML();
$obj = new XmlParseRequest();
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$this->assertEquals("5", $pr->body->id, "[new pending game]");
$this->assertEquals($session2, $pr->body->session, "[new pending game]");
$this->AssertEquals(1, game::getNumberOfGames(), "two pending games with the same size added[new pending game]");
$obj->processRequest($req);
$response = $obj->getResponse();
$pr = simplexml_load_string($response);
$this->assertEquals("successful", $pr->body->status, "[new pending game]");
$this->assertEquals("5", $pr->body->id, "[new pending game]");
$this->assertEquals($session2, $pr->body->session, "[new pending game]");
$this->AssertEquals(2, game::getNumberOfGames(), "two pending games with the same size added[new pending game]");
}