我尝试在Codeigniter中使用session来做登录页面。
为了达到这个目的,我做了一个模型
function girisKontrol($username, $password) {
$sha1_password = sha1($password);
$query = "SELECT id FROM pasaj_register WHERE username = ? and password = ?";
$result = $this->db->query($query, array($username, $sha1_password));
if ($result->num_rows() == 1)
return $result->row(0)->id;
else
return false;
}在giris控制器中,我创建了一个名为giris的函数
public function giris() {
extract($_POST);
$userID = $this->giris->girisKontrol($username,$password);
echo $userID;
if (!$userID) {
$this->session->set_flashdata('login error',TRUE);
redirect('giris/giris');
} else {
$this->session->set_userdata(array(
'logged_in' => TRUE,
'userID' => $userID));
redirect('welcome_message');
}
}在同一个控制器中,我多做了一个函数:
public function main_page()
{
if ($this->session->userdata('logged_in'))
echo "Logged in";
else
echo "Error";
}并在视图中使用所有这些
<form method="POST" action="http://localhost:81/pasaj/giris/main_page/" name="flogin" autocomplete="off">
<label for="username"><b>Kullanıcı adı</b> ya da <b>e-posta</b> adresiniz:</label>
<input type="text" value="" class="normalinput" name="username" id="username">
<label for="password">Şifreniz:</label>
<input type="password" class="normalinput" value="" name="password" id="password">
<span class="bigsubmitwbtn yellow fright"><span class="bigsubmitwbtn_left"></span><input type="submit" class="bigbutton" name="submit" value="Giriş"></span>
</form>
</div>然而,正如在main_page函数中解密的那样,它直接转到else语句并打印错误为什么?
发布于 2013-03-16 18:56:02
尝试加载会话,例如在应用程序/配置文件夹中的autoload.php中:
$autoload['libraries'] = array('session');或者在你的控制器的构造函数中。
还要确保在config.php中正确设置了会话,如下所示:
$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 86400;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;如前所述,将$config‘’sess_cookie_name‘从:
$config['sess_cookie_name'] = 'ci_session';至:
$config['sess_cookie_name'] = 'cisession';您的会话设置是否正确,例如:
// create session
$this->session->set_userdata($data);如何存储会话数据?是否在数据库中?
发布于 2012-02-29 22:07:56
在codeigniter配置中是否更改了默认会话/cookie名称和域?
请确保更改名称时包含下划线"_“,因为下划线并不适用于所有浏览器,例如IE ;)
https://stackoverflow.com/questions/9500234
复制相似问题