CI里怎么用cookie实现记住密码功能
急~~请各位大大发个源码看看 和一般的 PHP 程序能有什么不同呢? 我已经设置了cookie失效时间为一个月~但是关闭浏览器之后怎么没有在登录框内显示保存在cookie里的用户名呢 你设置显示到登录框里了吗?而且还要注意 cookie 的 path 是的~~而且path我也设置了~还是不好使/* * 用户登陆模块 * */
function user_login_act()
{
$username=mysql_escape_string(trim($this->input->post('username')));
$password=trim($this->input->post('password'));
$savepwd=trim($this->input->post('savepwd'));//记住密码
$sql="select * from omy_user where username='".$username."' and password='".md5($password)."'";
$result=$this->db->query($sql);
if($result->num_rows()>0)
{
if($savepwd=='savepwd')
{
$user = array('name'=>'username','value'=>$username,'expire'=>60*60*24*30,'domain' => base_url(),'path'=> '/',);
set_cookie($user);
//$pwd=array('name'=>'password','value'=>$password,'expire'=>60*60*24*30);
// set_cookie($pwd);
}
$row=$result->row_array();
$uid=$row['uid'];
$sess_arr = array('uid' => $uid,'username' =>$username);
$this -> session -> set_userdata($sess_arr);
$hint = '登陆成功';
$url = site_url('user_manage/user_center').'/'.$uid;
exit("<script>\nwindow.alert('{$hint}')\nwindow.location = \"$url\";\n</script>");
}else
{
$hint = '登陆失败';
$url =base_url();
exit("<script>\nwindow.alert('{$hint}')\nwindow.location = \"$url\";\n</script>");
}
}
你看看这段代码对不对 'domain' => base_url() 这样写不对,直接写域名 'domain.com',或者去掉这个使用 config 里的默认值,推荐使用默认值。
没发现哪里设置 cookie 到编辑框中。 本帖最后由 黄巧龙 于 2009-6-11 18:31 编辑
// 首页控制器~default_index.php
<?php
class Default_index extends Controller
{
function Default_index ()
{
parent::Controller();
$this->load->model('common_fnc');
$this->load->helper('cookie');
}
function index ()
{
$data['username']=get_cookie('username', TRUE);
//$data['password']=get_cookie('password', TRUE);
$this->load->view('index',$data);
}
}
?>
//登陆框 index.php
<table border="0" width="100%" cellpadding="5" cellspacing="5">
<form action="<?=site_url('user_manage/user_login_act')?>" method="POST" name="loginform">
<tr>
<tdwidth="150">用户名:<input name="username" type="text" size="20" value="<?php if(!empty($username)) echo $username?>"></td>
<td width="150">密 码:<input name="password" type="password" size="20"></td>
<td width="100"><input name="login" type="submit" value="登陆"></td>
<td width="100"><input name="savepwd" type="checkbox" value="savepwd">记住密码</td>
<td width="150" ><a href="<?= site_url('user_manage/user_register')?>">注册</a></td>
<td width="150"><a href="<?= site_url('findpassword/index')?>">找回密码</a></li></td>
</tr>
</table>
</form> 只用set_cookie('username',$username,3600*30*24)看看,
另外,下面两个效果是一样的:
用户名:<input name="username" type="text" size="20" value="<?php if(!empty($username)) echo $username?>">
用户名:<input name="username" type="text" size="20" value="<?php echo $username?>">
其实楼主已经使用js了,可以直接用js填充username框框啊,节省服务器开销
页:
[1]