set cookie in laravel 5.4 - php

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?

Related

else condition is working even if is true

I'm using codigniter 3.1.2 and using simple if condition in php but really confused when I checked my condition is true getting result from else condition as well and if as well mean getting data in from both cases.
public function editexperience($UserID = 0, $ID = 0) {
if ($this->Access->HaveAccess('Edit')) {
$Row = $this->Experience->Row($ID);
if ($ID > 0 && $Row){
print_r($Row);
Def(array(
'Update'=>'users/submit/updateexperience/'
),'Actions');
$User = $this->Model->Row(intval($UserID), '', 'CTRY.Currency');
$ID = intval($ID);
$Data['Row'] = $Row;
Def($Row,'Row');
$Data['Title'] = 'Edit Experience';
$Data['Fields'] = $this->GetExperienceFields($User->Currency, true);
$this->Main->SetPage('Helper/Edit', $Data, true);
} else {
TempNote('There is problem while getting request data!', 'Error');
redirect(URI(_E('{Page.List}'), true), 'refresh');
die();
}
}
}
In code I mentioned if I have Row then load template of edit.
Suppose:
I have Row and its loading edit template and also showing the error of else condition really confuse what is happening.
My PHP version is 5.6.31
TempNote Function
function TempNote($Message, $Type) {
//Prepare Data
$Current = array('Message'=>$Message,'Type'=>$Type);
//Get current instance
$Ins = & get_instance();
$Old = $Ins->Main->Messages;
// If another is already set and that should not be current
if (!empty($Old)) {
foreach ($Old as $Key=>$Now) {
if ($Now === $Current){
unset($Old[$Key]);
}
}
}
$Ins->Main->Messages[] = $Current;
$Ins->session->set_flashdata('Message', $Ins->Main->Messages);
}

Loop the PHP Code Until Statement Is True

I'm using two API keys to receive the data. That's because provider have some daily quota limit. If quota exceeds the daily limit, 403 error is returned from the server.
I used $http_response_header to check the response from the server. But the problem is that when response returned isn't 200, e.g the API key used exceeds the daily limit, the code should execute again so that another API key is picked randomly. Here's the code I'm using:
$keys = array('1stkey','2ndkey');
$key_r = array_rand($keys);
$yt_key = $keys[$key_r];
$data = file_get_contents("https://example.com/data&key=$yt_key");
if (strpos($http_response_header[0], "200")) {
echo $data;
}
else{
echo 'No';
}
You can create a function of this code, and just re-run the function when the response is not 200. Something like this maybe?
function getAPIData() {
$keys = array('1stkey','2ndkey');
$key_r = array_rand($keys);
$yt_key = $keys[$key_r];
$data = file_get_contents("https://example.com/data&key=$yt_key");
if (strpos($http_response_header[0], "200")) {
echo $data;
}
else {
getAPIData();
}
}
Like #Jaquarh says, you might want to change max_execution_time also. Have a look here and see what works for you.
I've created perfect solution, check this:
$api = array('1st key','2nd key');
$k = array_rand($api);
$api_key_yt = $api[$k];
$total_api=count($api);
for ($loop=0;$loop<=$total_api;$loop++) {
$api_key_yt=$api[$loop];
$request2 = "https://example.com/?data&key=$api_key_yt";
$response = file_get_contents($request2);
$jsonobj = json_decode($response);
if (isset($jsonobj->items)) {break;} else {unset($jsonobj);}
}
print_r($jsonobj);
Please try this
$data = null;
do {
$keys = array('1stkey','2ndkey')
$key_r = array_rand($keys);
$yt_key = $keys[$key_r];
$data = file_get_contents("https://example.com/data&key=$yt_key");
} while ($data == null);

Exception: Required option not passed: access_token

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'];
}

Login with User-Agent as token

I'm working on a website project (PHP, Fat-Free and mongodb) and I have a problem with only one user.
When the user does login, I call this function:
function login() {
$user = F3::get('POST.user');
$pass = F3::get('POST.pass');
if($user!=""&&$pass!=""){
$bcrypt = new Bcrypt(12);
$ismail = strrpos($user, "#");
$loginUser = new HT('user');
if($ismail===false){
$loginUser->load(array('nick' => $user));
}else{
$loginUser->load(array('email' => strtolower($user)));
}
if(!$loginUser->dry()){
if($loginUser->isActive == true){
$isValid = $bcrypt->verify($pass, $loginUser->password);
if($isValid){
$final_token = StringHelper::rand_str(30);
$tmp = $loginUser->token;
if(!is_array($tmp))
$tmp = array();
$cli_id = md5($_SERVER['HTTP_USER_AGENT']);
$tmp[$cli_id] = $final_token;
$loginUser->token = $tmp;
$user_id = $loginUser->_id."";
$loginUser->loginDate = new MongoDate();
$loginUser->updateFromId();
$isPremium = $loginUser->isPremium;
F3::set('user_id', $user_id);
if(isset($loginUser->preferences) && isset($loginUser->preferences['langPref'])){
$lang = $loginUser->preferences['langPref'];
echo json_encode(array("status"=>"OK","id"=>"$user_id","token"=>"$final_token","langPref"=>"$lang","schema"=>"$schema","isPremium"=>$isPremium));
}else{
echo json_encode(array("status"=>"OK","id"=>"$user_id","token"=>"$final_token","langPref"=>"en","schema"=>"$schema","isPremium"=>$isPremium));
}
}else{
echo json_encode(array("status"=>self::NOT_EXIST));
}
}else{
echo json_encode(array("status"=>self::NOT_ACTIVE));
}
}else{
echo json_encode(array("status"=>self::NOT_EXIST));
}
}
}
NOTE: dry() function checks if user is null in mongodb.
The user says that in his office computer can't do login, and he gets and "Invalid password" error. But he can login in the website in any other computer.
Sometimes, if he deletes cache or reset his password can access to the website.
I'm saving in the 'User' mongodb collection an array of tokens like this:
{
........ (Other params),
"token" : {
"d222b6a854d35c9c9584e695b623c468" : "nX0CuAraUQMGm5UsUWhUqZzgXZnapN",
"8c27b0d66a7ea6cec5dda9979cc92bd3" : "swdYhSQN5el0x9Pmn2YXZuwckpmuHP",
"28b825f204e7ebd320756bd6294a29c1" : "UFGd18db8W9gq1WDXgx4F9BZRVRY3K"
}
}
This token array contains:
key: MD5 encrypted User-Agent
value: Random string
This keeps session for different devices and browsers.
Everytime the user wants to do something in the web I put in the route the user id and the token (value), and I check if values are correct:
....
$uid = md5($_SERVER['HTTP_USER_AGENT']);
if(isset($user->token[$uid]) && $user->token[$uid]==$token){
....
return true;
}else{
return false;
}
I thought that the problem was with the User-Agent. Any solution?
Thanks!

Can't obtain user likes in some cases using facebook graph API

I have a problem with a facebook app. I want to know if the user likes a page to show one content or another. The problem is that some users click on like button but the app dont read it from facebook graph API. Here is the code I'm using to check if a user likes the page:
$appID = '171296629176';
$resp = file_get_contents("https://graph.facebook.com/".$user."/likes?access_token=".$facebook->getAccessToken()."&limit=9999");
$data = json_decode($resp,true);
$liked = 0;
$likes_array = $data['data'];
$likes_next = '';
if(($data != null) && (array_key_exists('paging',$data)) && (array_key_exists('next',$data['paging']))){
$likes_next = $data['paging']['next'];
}
foreach($likes_array as $like)
{
if($appID == $like['id']){
$liked = 1;
}
}
while(strlen($likes_next) > 0){
$resp = file_get_contents($likes_next);
$data = json_decode($resp,true);
$likes_array = $data['data'];
$likes_next = '';
if(($data != null) && (array_key_exists('paging',$data)) && (array_key_exists('next',$data['paging']))){
$likes_next = $data['paging']['next'];
}
foreach($likes_array as $like)
{
if($appID == $like['id']){
$liked = 1;
}
}
}
In some cases it isn't working. I don't know if facebook API returns an empty list of likes or if it returns an incomplete list. With my account and others it works ok and I can't contact the users that are experiencing the issue to test the app.
Thanks in advance.
Alfonso.

Categories