I am quite new to Symfony, and I want to set up a website were an user can select different board to display. All the board are in my twig template and are hidden if the value of the cookie is 0. If the user click on the menu, the value of the cookie will change to 1, displaying the board.
However, when I click on the menu, the first time, it does not change anything, but the second time, it work perfectly.
This is how I set up my cookie:
$var = 0;
$response = new Response();
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->send();
And this is how I change the value of the cookie:
$response = new Response();
$cookie = $this->getRequest()->cookies->get('stream');
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
return $response;
Edit: Here is my controller
public function streamAction()
{
$advert = $this->getAdvertEntity();
$stream = $this->getStreamEntity();
$cookie = $this->getRequest()->cookies->get('stream');
if (!isset($cookie) || $cookie == 0) {
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->sendHeaders();
return $this->redirectToRoute('stream', array(
"adverts" => $advert,
"liststream" => $stream
));
}
$content = $this->get('templating')->render('IVPlatformBundle:Advert:Advert.html.twig', array(
"adverts" => $advert,
"liststream" => $stream
));
return new Response($content);
}
I really don't know what is wrong, so any help will be nice :)
Thanks
If you first time visit the route, cookie is not set in request, you receive it in response. Solution is to make redirect to the same route if cookie changed:
$cookie = $this->getRequest()->cookies->get('stream');
if (!isset($cookie) || $cookie == 0) {
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->sendHeaders();
return $this->redirectToRoute('actual_route');
}
// Do other stuff if cookie set to 1.
return $response;
Related
I am trying to set few cookie variables and redirect to next route.
$inputParams = [];
$inputParams['show_match_skus'] = $this->request->input('show_match_skus');
$inputParams['show_all'] = $this->request->input('show_all');
$inputParams['prevent_dialog'] = $this->request->input('prevent_dialog');
$inputParams = array_map(array($this, 'sanitizeInputParams'), $inputParams);
$filterArr = array_filter($inputParams);
if(count($filterArr) > 0) {
if(array_key_exists('show_match_skus', $filterArr)) {
$cookie_sku_match = Cookie::make('show_match_skus', true);
}
if(array_key_exists('show_all', $filterArr)) {
$cookie_all = Cookie::make('show_all', true);
}
if(array_key_exists('prevent_dialog', $filterArr)) {
$cookie_prevent = Cookie::make('prevent_dialog', true);
}
}
//dd(Cookie::get());
//dd(Cookie::get('show_all'));
return redirect()->route('list');
After creating cookies I need to redirect to list route.
But on checking in browser, I get none.
Also Cookie::get() displays only XSRF-TOKEN and laravel_session.
But Cookie::get('show_all') shows the cookie.
What am I doing wrong?
I am getting this error while trying to login with Google in Codeigniter only at first attempt
Fatal error: Uncaught exception 'Exception' with message ' in ..../application/libraries/OAuth2/Token/Access.php on line 44
Exception: Required option not passed: access_token in ..../application/libraries/OAuth2/Token/Access.php on line 44
Try to change your access.php file and change __construct method.
public function __construct(array $options = null)
{
echo "<pre>";
$new_options = array();
foreach($options as $key => $value)
{
$new_options = json_decode($key,true);
}
if ( ! isset($new_options['access_token']))
{
throw new Exception('Required option not passed: access_token'.PHP_EOL.print_r($new_options, true));
}
// if ( ! isset($options['expires_in']) and ! isset($options['expires']))
// {
// throw new Exception('We do not know when this access_token will expire');
// }
$this->access_token = $new_options['access_token'];
// Some providers (not many) give the uid here, so lets take it
isset($new_options['uid']) and $this->uid = $new_options['uid'];
//Vkontakte uses user_id instead of uid
isset($new_options['user_id']) and $this->uid = $new_options['user_id'];
//Mailru uses x_mailru_vid instead of uid
isset($new_options['x_mailru_vid']) and $this->uid = $new_options['x_mailru_vid'];
// We need to know when the token expires, add num. seconds to current time
isset($new_options['expires_in']) and $this->expires = time() + ((int) $new_options['expires_in']);
// Facebook is just being a spec ignoring jerk
isset($new_options['expires']) and $this->expires = time() + ((int) $new_options['expires']);
// Grab a refresh token so we can update access tokens when they expires
isset($new_options['refresh_token']) and $this->refresh_token = $new_options['refresh_token'];
}
If you are using oauth2, goto libraries/oauth2/provider.php there is function called access, in that switch case: POST, do it what i have shown below.
case 'POST':
$ci = get_instance();
/*$ci->load->spark('curl/1.2.1');
$ci->curl
->create($url)
->post($params, array('failonerror' => false));
$response = $ci->curl->execute();*/
Happy coding
public function __construct(array $options = null)
{
$new_options = array();
foreach($options as $key => $value)
{
$new_options = json_decode($key,true);
}
if($new_options==null || $new_options=="")
{
$new_options=$options;
}
if ( ! isset($new_options['access_token']))
{
throw new Exception('Required option not passed: access_token'.PHP_EOL.print_r($new_options, true));
}
// if ( ! isset($options['expires_in']) and ! isset($options['expires']))
// {
// throw new Exception('We do not know when this access_token will expire');
// }
$this->access_token = $new_options['access_token'];
// Some providers (not many) give the uid here, so lets take it
isset($new_options['uid']) and $this->uid = $new_options['uid'];
//Vkontakte uses user_id instead of uid
isset($new_options['user_id']) and $this->uid = $new_options['user_id'];
//Mailru uses x_mailru_vid instead of uid
isset($new_options['x_mailru_vid']) and $this->uid = $new_options['x_mailru_vid'];
// We need to know when the token expires, add num. seconds to current time
isset($new_options['expires_in']) and $this->expires = time() + ((int) $new_options['expires_in']);
// Facebook is just being a spec ignoring jerk
isset($new_options['expires']) and $this->expires = time() + ((int) $new_options['expires']);
// Grab a refresh token so we can update access tokens when they expires
isset($new_options['refresh_token']) and $this->refresh_token = $new_options['refresh_token'];
}
I have the following model:
// Session info in $session array.
$session = $query->row_array();
$userid = $session['uid'];
$uq = $this->db->query("SELECT * FROM `administration_users` WHERE id = $userid");
$now = strtotime(date("m/d/Y h:i:s A"));
if($session['terminatedate'] > $now) {
// If Session is STILL ACTIVE, let's give them an additional hour. Have fun!
$newterm = $now + 3600;
$userq = $uq->row_array();
$username = $userq['username'];
$userfull = $userq['name_first'] . " " . $userq['name_last'];
$this->ELog->authentication('INFORMATION',$username,"Session for $userfull has been extended by 3600 seconds (1 hour) due to activity in application.");
$this->db->query("UPDATE `administration_sessionkeys` SET terminatedate='$newterm' WHERE id='$session[id]'");
} elseif($session['terminatedate'] < $now) {
// If Session is OVER...
$userq = $uq->row_array();
$username = $userq['username'];
$userfull = $userq['name_first'] . " " . $userq['name_last'];
$this->ELog->authentication('INFORMATION',$username,"$userfull has been logged out automatically by AuthLogger due to session timeout. Thanks for stopping by. Goodbye!");
$this->db->query("UPDATE `administration_sessionkeys` SET `terminated`='1',`terminated_details`='Session Ended. Thanks for stopping by. Goodbye!' WHERE id='$session[id]'");
setcookie("ssmp_auth_cookie_uid", "", time() - 3600);
return false;
}
$userinfo[] = $uq->row_array();
return array($userinfo);
My controller, looks like this:
$this->load->model('Auth_model', "", TRUE);
$data = array($this->Auth_model->authcheck());
if(!$data) {
header("Location: /auth/login");
} else {
$this->load->view('tmpl/header', $data);
$this->load->view('dashboard', $data);
$this->load->view('tmpl/footer', $data);
}
My view is as such, and the problem here is that I can't access my array from the model in the view. What am I doing wrong? I've been racking my brain for 3 hours now on this. I need a break and some help.
<title>Logged into SSMP as <?=$id;?></title>
This should give me the "id" column in the array, right? No?
Please help me out here, sorry for so much code, I wasn't sure what you guys needed to see in order to help me out.
Thanks for all that are able to provide assistance with this irritating issue.
Scott
row_array() already returns an associative array, so you don't need to wrap the results in an array again
change
$userinfo[] = $uq->row_array();
return array($userinfo);
to
$userinfo = $uq->row_array();
return $userinfo;
Change the following :
$userinfo[] = $uq->row_array();
return array($userinfo);
to :
return $uq->row_array();
and :
$data = array($this->Auth_model->authcheck());
to :
$data['administration_users'] = $this->Auth_model->authcheck();
In you view file :
<title>Logged into SSMP as <?php echo $administration_users['id']; ?></title>
Tips: use redirect helper for redirection :
if(!$data) {
// header("Location: /auth/login");
// Assuming that you have url_helper already loaded,
// if not, uncomment the next line :
// $this->load->helper('url');
redirect('auth/login');
I have issue regarding codeigniter Timeout .
I know the config folder setting session timeout manually like as l
$config['sess_expiration'] = 123;
but i need to the website admin manage the session time out dyanamicly in to the admin page
please help me how to implement this logic
i tried this logic but not working
$this->session->sess_expiration = "120";
Note:here i am storing database in the value. based on the database value i can set in to the session expiration time
note 1: $config['sess_time_to_update'] = 30; this value less than of session expiration time
Total Logic code:
public function edit($id)
{
Assets::add_css('../plugins/forms/uniform/uniform.default.css');
Assets::add_css('../plugins/forms/select/select2.css');
Assets::add_css('../plugins/forms/validate/validate.css');
Assets::add_css('../plugins/misc/qtip/jquery.qtip.css');
Assets::add_js('../plugins/charts/sparkline/jquery.sparkline.min.js');
Assets::add_js('../plugins/forms/uniform/jquery.uniform.min.js');
Assets::add_js('../plugins/forms/select/select2.min.js');
Assets::add_js('../plugins/forms/validate/jquery.validate.min.js');
Assets::add_js('../plugins/forms/wizard/jquery.bbq.js');
Assets::add_js('../plugins/forms/wizard/jquery.form.js');
Assets::add_js('../plugins/forms/wizard/jquery.form.wizard.js');
Assets::add_module_js('setting','setting');
if ($_POST)
{
$current_date = date("Y-m-d H:i:s");
$data = array(
's_meta_value' => $this->input->post('s_meta_value'),
'updated_on' => $current_date
);
$this->setting_model->session_mng_update($data,$id);
$session_val= $this->input->post('s_meta_value');
if($session_val == 0)
{
$this->session->sess_expiration = '0';
}
else
{
$this->session->sess_expiration = "120";
// $val1 = $this->config->item('sess_expiration');
// print_r($val1);
//$session_seconds = ($session_val*60);
$val2 = $this->config->set_item('sess_expiration',50);
$this->session->CI_Session();
//$val1= $this->config->set_item('sess_expiration',50);
$val3 = $this->config->item('sess_expiration');
print_r($val3);exit;
}
Template::redirect('setting/setting/display');
}
$val3 = $this->config->item('sess_expiration');
print_r($val3);exit;
$data = $this->setting_model->session_mng_edit($id);
Template::set('page_title', 'Edit Session Management');
Template::set('data', $data);
Template::set_view('setting/session_management/edit_session_management');
Template::render();
}
First of all you could set cookies for this process via $this->input->set_cookie(). But if you want to overwrite the config variable then try
$this->config->set_item('sess_expiration',120);
Also call the session constructor to update the value by
$this->session->CI_Session();
You should use something like this:
$remember_me = $this->input->post('remember_me');
if ($remember_me == 'remember_me')
{
//set session to non-expiring
$this->session->sess_expiration = '32140800'; //~ one year
$this->session->sess_expire_on_close = 'false';
#$this->session->set_userdata($session_data);
}
else
{
//set session expire time, after that user should login again
$this->session->sess_expiration = '1800'; //30 Minutes
$this->session->sess_expire_on_close = 'true';
#$this->session->set_userdata($session_data);
}
//set session and go to Dashboard or Admin Page
$this->session->set_userdata(array(
'id' => $result[0]['id'],
'username' => $result[0]['username']
));
i have this code for creating cookie
This works in firefox and crome browser but in IE it is creating cookie again and again
if (!isset($_COOKIE["cook"])) {
$expire = time() + 60 * 60 * 24 * 30 * 2;
$data = array(
"ip" => $_SERVER['REMOTE_ADDR'],
"browser" => $_SERVER['HTTP_USER_AGENT'],
"create_time" => $now
);
$result = $db->insert("cookies", $data);
$cookie_id = $db->lastid;
$cookie_id = my_encrypt($cookie_id);
setcookie("cook", $cookie_id, $expire,"/","localhost");
} else {
$cookie_id = $_COOKIE["cook"];
}
Everytime I visit page it creates new cookie
omit the domain-parameter for setcookie()