Good day,
I have been struggling with this for the past few hours but admittedly I'm not very familiar with Laravel.
I use this row of code to generate a cookie:
return Response::make(view("/play"))->withCookie(cookie('username', $request->input('username'), 60000));
I have logged $request->input('username') and it is not empty,
the application goes to the /play view but when I use Cookie::get('username')in a later method in another Controller later on it returns null.
picture of cookie storage it also does not appear empty in the browser.
Does anyone know why this is happening and how to fix it?
try Request()->cookie('username')
Related
I have a very special problem and I don't know how to deal with it.
I have web App in Laravel, when i open index page, I receive text message to my mobile phone.
Problem is, sometimes I receive 2 messages or 3, sometimes 1.
Is there a tool how to debug this strange behavior which is not always the same?
A few words about my code:
user opens the page, and because its first visit Session doesn't have attribute message_sent and SendTextMessage::SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat); is executed. After that Session has message_sent and can't be sent again, for example if I refresh the page.
SendTextMessage::SendMessage() is Class in Laravel Helpers.
controller code:
public function index($url_attribute, $id_message, Request $request)
{
if(!Session::has('message_sent'))
{
$user = User::where('id_message', $id_message)->first()->toArray();
$phoneNumber = $user['mobile_phone'];
$smsCode = $user['sms_code'];
$newDateFormat = date("d.m.yy", strtotime($smsExpirationTime));
$request->session()->flash('message', 'Text message sended.' );
SendTextMessage::SendMessage($phoneNumber,$id_message, $smsCode, $newDateFormat);
Session::put('message_sent', true);
}
return view('login');
}
SendTextMessage Class:
class SendTextMessage
{
public static function SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat)
{
$sms = new Connect();
$sms->Create("user","pass",Connect::AUTH_PLAIN);
$sms->Send_SMS($phoneNumber,"Message");
$sms->Logout();
}
}
Many thanks for any tip or help.
UPDATE:
problem is only in Chrome.
Edge and internet explorer are fine.
As this script runs on server-side the browser shouldn't be an issue. Based on your code provided, there is no clear answer to give here.
Please try the following in order to debug your problem:
Log messages at each stage of the script in order to see which part was called how often. That will help you to locate the problem. You can use \Log::error("Message") to do that.
Once you know where the problem might be, try to log "decision" making / mission critical variables to logile as well. E.g. \Log::error($session) so that you can understand why that problem might occur. One reason could be that you have a bad configured session caching or your cookies might be messed up. At some point there is probably a piece of data not the way you expect it to be.
You should maybe try to change the way you use Laravel Session.
You indicated that it was working fine on some browsers, that means your server-side code is correct so far, but there is someting messing with Chrome… From there,
if you take a quick look at the Laravel Session doc, you'll see that Session can be stored in cookies, and I bet that this is your actual setup (check in your .env file the SESSION_DRIVER constant, or in your config/session.php file).
If so, to confirm that this cookies-based session setting is the culprit, you might want to change the Session config to make it browser-independent: any other option than cookies will work, the database or file options might be the easier to setup… And if it works I would strongly encourage you to keep using this no-cookie setting to make your code browser-safe.
I am trying to make a test transaction using my Laravel 7 app and Authorize.net.
After submitting the sample data, I'm getting:
The element 'createTransactionRequest' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element 'clientId' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. List of possible elements expected: 'merchantAuthentication' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.
Anyone know what's causing this error or how to fix it?
Well, I'll answer my own question since it might help others. The problem is the error message in the Authorize.net response is really vague.
Kurt Friars' comment was helpful, since it pointed me in the right direction. As for Mansour Hamcherif's suggestion, the merchantAuthentication object was set in my app, it just didn't have the right values, so it wasn't that.
The solution for me was setting the proper values to setName() and setTransactionKey(). The previous developer who had worked on this project had left values and the credentials had expired. I did a Linux text search for "setTransactionKey", which lead me to the correct PHP file where I need to set:
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName('EnterYourLoginNameHere');
$merchantAuthentication->setTransactionKey('EnterYourTransactionKey');
After that, I cleared all of my Laravel app's caches as well as my browser's caches, did a hard reload, tried a transaction again and it worked! I got:
This transaction has been approved., Transaction ID: **********.
You may want to check the log for the raw request, it's likely the merchantAuthentication object has not been set, if you are using the PHP SDK I recommend checking the SimpleCodeConstants.php file and make sure your merchant credentials constants are set.
For example, if I set my merchant credentials to NULL, I get the same E00003 error as a result of sending the following raw request:
{"createTransactionRequest":{"merchantAuthentication":[],"clientId":"sdk-php-2.0.0-ALPHA", ...}
I'm trying to get sessions to work in Laravel 5.7, but I can't seem to get it working; I'm not sure why. I've looked at the official docs, looked at a bunch of SO posts, done Googling for the issue on other sites, and I still can't figure it out.
Basically, I've got a route like the following in web.php:
Route::get('/some_path', 'SomeController#index');
Then, in the index method of SomeController, I have the following:
session(['test', 'Some Value']);
session(['test2', 'Some Other Value']);
session(['test3', 'Some Third Value']);
$value = session('test', 'Backup Value');
echo $value;
With that, I'm always getting Backup Value echoed to the screen.
If I go into /storage/framework/sessions, I see a session file that has the following content (which I have anonymized):
a:5:{s:6:"_token";s:40:"token-here";s:9:"_previous";a:1:{s:3:"url";s:26:"http://example.com/some_path";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}i:0;s:5:"test3";i:1;s:16:"Some Third Value";}
Basically, it looks like the session is kind of working, but it only seems to store test3. If I comment out the test3 line in the controller, then it only stores test2. And even if I only have the test line in the controller, it still doesn't retrieve the value.
I also tried switching the session to a DB session with the proper session migrate file a la https://laravel.com/docs/5.7/session, but it's still not working.
Basically, I have no clue what's up. I can say that I'm not using the native Laravel auth, so I'm not sure if that matters, but I feel like it shouldn't. Also, I tried restarting my localhost, but that didn't make a difference and running both of the following commands, neither of which seem to change anything:
php artisan config:clear
php artisan route:clear
Lastly, I tried adding use Session at the top of the controller, but that doesn't seem to matter either.
Basically, I don't know what to do. What could possibly be causing this? Thank you.
What's happening here is that when you add this:
session(['test', 'x'])
It adds test at the 0th index of the session and x at 1st.
Now when you again do a session(['test1', 'xx'])
It overwrites the 0th and 1st index of session with new values.
Therefore when you print the session data you get the last values.
You can check this by doing a dd(session()->all()) and seeing the same on screen.
If you want to make a key value relation and store the data in such away, please use the syntax like this:
session(['test' => 'x']);
session(['test1' => 'xx']);
those who are unable to save the session the issue is you guys are forgot to put the save() function after put given is the example.
$cartCount = Session::get('cartCount');
if(empty($cartCount)){
$cartCount = 0;
Session::put('cartCount', $cartCount);
}
Session::save();
Session::save() can be a life saver, definitely try that if still issues.
Laravel 5.7 - store value in session with File system:
Set the value in session:
session()->put('email', 'jai#gmail.com');
Get the value from session:
$email = session()->get('email');
Delete the session of email:
session()->forget('email');
It works like charm :)
If for some reason your code doesn't run through - eg you're using dd() somewhere, then your session will never save. This tripped me up for a good 20 minutes.
In this case, all you need to do is add a "web" middleware chain function to your route rule.
e.g
Route::get('/some_path', 'SomeController#index')->middleware('web');
Then clear cache:
php artisan config:clear
php artisan route:clear
I have an issue with post variables in Codeigniter, for now I fixed it, but I'm not sure if this is fine, in other words, I'd really like to know why I had issue only on hosting server, not on local.
The problem:
I wanted more secure script so I replaced $_POST/$_GET with $this->input->post and $this->input->get, like in this example:
if(!empty($this->input->get('endDate'))){
$data['datepicker'] = $this->input->get('thisDate');
}
this was all working fine on my localhost, but I assume it is wrong way, as when I deployed to the hosting server, I've got ajax parse error from validation engine that I'm using, I couldn't log in the system, getting all the time ajax parse error, and somehow figured out that whenever I checked if empty$this->input->post('var) or the same with get, was wrong.
I have checked the input class from Codeigniter and figured out, that $this->input->post is returning true/false, so I changed the scripts in the format like this:
if(!($this->input->get('endDate'))){
$data['datepicker'] = $this->input->get('thisDate');
}
this worked fine.
I'm still wondering - why the code worked on the localhost, but it didn't on the server or what is the best way to check if the POST/GET variable is empty or not.Seems I really miss something out.
Thank you.
Consider that checking for empty($this->post->get('var')) will always return false, no matter if the POST/GET exists, so it might be for that reason only. - So i'm not sure why you got the error on a different environment, but the first statement is wrong, it's exactly like writing if(true)
Also, I'd check for a strong assertion of false for inputs, You never know when you might need a zero as an input and if you're used to this practice, you might get an accidental false.
I am new to cakePHP.t I thought this should be simple but I am having hard time to find the reason why saving in a controller action does fail.
I tried to print error
if ($this->MembershipRequest->save($this->data['MembershipRequest'])){
$this->flash("All items are saved", array("controller"=>"home", "action"=>"index"));
} else {
pr($this->MembershipRequest->validationErrors);
}
But it does not print anything. Could anybody give me right direction?
I am looking for a good way of debugging cakephp app. One thing I just found out is Configure::write('debug',...) which gives great debugging messages. Is there any other tool? Thanks
I'm not sure of your format of $this->data, but I'm pretty sure you don't need to pass the "MembershipRequest' as a key in the save, try saving with $this->MembershipRequest->save($this->data) and it should work.
Also, check the beforeSave() methods you have in your MembershipRequest or AppModel, they should always return true, otherwise the save will silently fail.
For other debugging help, I suggest you look at the CakePHP DebugKit.