Well I have created sessions but having trouble implementing cookies in my website. In config file i have set $config['sess_expire_on_close'] = TRUE; so that user's session expires on closing browser.
Now what i want is, On login if user checks remember me ... all data should be stored in a cookie so that on closing browser, user is still logged on.
function login($email, $password, $loginas) {
if ($loginas == 'user') {
$this->db->select('*');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get("user_info");
if ($query->num_rows() > 0) {
foreach ($query->result() as $rows) {
$newdata = array('id' => $rows->id,
'firstname' => $rows->firstname,
'lastname' => $rows->lastname,
'address' => $rows->address,
'city' => $rows->city,
'email' => $rows->email,
'phone' => $rows->phone,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
if ($rememberme == 'on') {
// create a cookie here with all data that i have put in session
}
return TRUE;
}
return FALSE;
}
}
Does creating cookie automatically creates session? or we have put these data in session manually again?
In CodeIgniter, you can use set_cookie()
$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.example.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
First of all you need to load cookie helper
$this->load->helper('cookie');
Set your cookie
$cookie = array(
'name' => "cookieName",
'value' => array('id'=>$rows->id,
'firstname'=>$rows->firstname,
'lastname'=>$rows->lastname,
'address'=>$rows->address,
'city'=>$rows->city,
'email'=>$rows->email,
'phone'=>$rows->phone,
'logged_in'=>TRUE
) ,
'expire' => '86500',
);
Just pass your array into set cookie
$this->input->set_cookie($cookie);
And you can retrieve it using
$cookie['cookieName']['id'];
Also read manual
Related
I'm using method setCookie to set login session on a website with Captcha, but method $page->cookies($url) doesn't return added cookie. How should cookies be set in PHP Puppeteer?
$page = $browser->newPage();
$page->setCookie([
'name' => 'login',
'value' => '***',
'domain' => 'www.domain.com',
'expires' => 0
]);
$page->setDefaultTimeout(20000);
$page->goto($url, ['waitUntil' => 'networkidle0']); // networkidle0
printf("url: %s\n", print_r($page->url(), true));
printf("cookies: %s\n", print_r($page->cookies($url), true));
I write this below code in a different controller to store cookies
$cookies = Yii::$app->response->cookies;
$number=0;
if($cookies->has('registration_id')){
if($cookies->has('registration_attempt')){
$registration_att = $cookies->getValue('registration_attempt');
$number = $registration_att+1;
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number,
'expire' => time() + 86400*2,
]));
}
else{
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number,
'expire' => time() + 86400*2,
]));
}
} else{
$cookies->add(new Cookie([
'name' => 'registration_id',
'value' => 'Generate Id',
'expire' => time() + 86400*2,
]));
$cookies->add( new Cookie([
'name' => 'registration_attempt',
'value' => $number+1,
'expire' => time() + 86400*2,
]));
//**show here**
}
I set secure true but it does not show anything in different controller . When i am trying to access cookies value it shows null. Cookie value set but it doesn't set globally . Instant show cookie value where I commented show here. I am checking here if cookie set then if either else and always hit that. And if echo in that place it show data .
Why codeigniter session destroyed after page re direction,
this is my auto load conf
$autoload['libraries'] = array('database','form_validation','session');
my login controller code is
$result = $this->Loginmodel->verify_user($this->input->post('username'), $this->input->post('password'));//returned as db->result()
if ($result !== False) {
//person has an account
foreach ($result as $obj) {
$user_id = $obj->user_id;
$userdesignation = $obj->user_designation;
$user_access = $obj->user_cpanelpass;
}
$sessiondata = array(
'user_id' => $user_id,
'username' => $username,
'loginuser' => TRUE,
'userdesignaton' => $userdesignation,
);
$this->session->set_userdata($sessiondata);
$sessiondata1 = array(
'shopname' => 'abc',
'shopplace' => 'xyz'
);
$this->session->set_userdata($sessiondata1);
$ipaddress = $this->ipaddress();
$this->Loginmodel->loginloginsert($username, $ipaddress);
redirect('home');
this is working fine in my system but not working in another system .The session is destroyed automatically when redirected to home or any other controller.
I am sending post requests in PHP to get a boolean value from my API (so it should return wither true or false)
This is the code I am using in the file for my API. The file is called users.php
if ($_POST['type'] == "authenticateMinecraft"){
$p = new dibdibs\post(
array(
'url' => 'https://authserver.mojang.com/authenticate',
'data' => array(
'agent' => array(
'name' => 'Minecraft',
'version' => 1
),
'username' => $_POST['username'],
'password' => $_POST['password'],
'clientToken' => "33225A179D9A4E1BDA73C012C1C3CBAB8BD00326883BDBEB6FA682482E40F68D"
)
)
);
$res = $p->json();
if (isset($res["selectedProfile"])){
echo("true");
}
else{
echo("false");
}
}
This is the code I am using to reference it (I am using a class which I have put on Pastebin to actually send the request).
$params = array(
'data' => array(
'type' => 'authenticateMinecraft',
'username' => $mcuname,
'password' => $mcpasswd
),
'url' => "api/users.php"
);
$c = new dibdibs\post($params);
$r = $c->http();
var_dump($r);
Whenever I use the .php fule extension when defining url, the whole PHP code of the API page is returned, but when I remove the extension, only true or false is returned. Why is this and is it a problem that I should be aware of and I should fox?
Working with the Yii framework in the config-file session storaged is handled as follows:
'session' => array(
//'sessionName' => 'SomeSession',
'class' => 'CDbHttpSession',
'connectionID' => 'SomeConnection',
'autoCreateSessionTable' => false,
'sessionTableName' => 'SomeTable',
'autoStart' => 'false',
'cookieMode' => 'only',
'useTransparentSessionID' => false,
'timeout' => CSESSIONTIMEOUT,
'cookieParams' => array(
'path' => '/',
'domain' => '.somedomain.extension',
'expire' => time()+5256000,
'lifetime' => time()+5256000,
//'httpOnly' => true,
),
),
So as you see sessions are stored in a table in a database with a given lifetime. But if I check the stored sessions in the database they are not stored with the given lifetime they are stored with a lifetime of a year.
The only thing I can find in our application that has a lifetime of a year are the cookies. For example like this:
setcookie("cookie_name", $someValue, time()+31536000, "/", "somedomain");
What is confusing for me are the cookies in our application. Could it be possible that this overrides the Yii session storage config?
UPDATE
I also came across this line of code
$_SESSION['POLL_'.$idPoll.'somekey'] = strtotime("now");
And that line of code inserted a session record in the database. But that record also has an lifetime of a year. How is this possible?
You need to add timeout param to config like this:
'session' => array(
'class' => 'CDbHttpSession',
'timeout' => 5256000,
// ...
Try Cookies Like this : -
if (isset($_POST['remember'])) {
$cookieUsername = new CHttpCookie('phoenix_admin_username', $_POST['LoginForm']['username']);
$cookiePassword = new CHttpCookie('phoenix_admin_password', base64_encode($_POST['LoginForm']['password']));
$cookieUsername->expire = time() + 604800;
$cookiePassword->expire = time() + 604800;
Yii::app()->request->cookies['phoenix_admin_username'] = $cookieUsername;
Yii::app()->request->cookies['phoenix_admin_password'] = $cookiePassword;
}
////////////Check like this//////////////
if(isset(Yii::app()->request->cookies['phoenix_admin_username'])){
$model->username = Yii::app()->request->cookies['phoenix_admin_username']->value;
$model->password = base64_decode(Yii::app()->request->cookies['phoenix_admin_password']->value);
}else{
$model->username = "";
$model->password = "";
}