Cookies disappearing after refresh in Cakephp 3
here is my HTML:
<a class="dropdown-item" value="1">Try1</a>
<a class="dropdown-item" value="2">Try2</a>
<a class="dropdown-item" value="3">Try3</a>
Lets have a look of my jQuery and ajax:
<script type="text/javascript">
$(document).ready(function() {
$(".dropdown-item").click(function(){
idString = $(this).attr("value");
alert(idString);
$.ajax({
type: "POST",
url: "<?php echo \Cake\Routing\Router::url(array('controller' => 'MyController', 'action' => 'index')); ?>",
data: "idString="+idString,
success: function(result){
$("#show").html(result);
}
});
});
});
Now lets see MyController:
$jqeryanimalid = $this->request->getdata('idString');
$this->Cookie->write('animalid', $jqeryanimalid,(time()+3600*24)+(mktime(24,0,0)-time()));
$cookie_animalid = $this->Cookie->read('animalid');
$posts = $this->Posts->find('all', array(
'conditions' => array(
'Animal_id' => $cookie_animalid
)
));
$this->set(compact('posts'));
The returning value is what i want and it save the Cookies like Name:animalid and Value:1 if i click Try1. The big Problem is that after refreshing all its gone also in cookies and the value that i want to have! Why? Can someone help me?
See also questions close to this topic
-
React Native infinite scroll problems with few elements
I'm using React Native with Firebase and I'm trying to use an infinite scroll (load more) on my Listview, everything works fine if the first fetch can fill the list for the entire height of the screen because onEndReached is fired but when there are few elements and the listview is not filled at all on the screen, onEndReached is not fired and so it doesn't download and fill with next records. Anyone can help me handling this situation? Thanks in advance.
-
Convert a cURL request to Javascript for an API call using hashed Authentication
I am a beginner at Javascript and APIs and I am trying to make PUT request with my credentials hashed with MD5 using Javascript which brings me back an Auth Code so that I can use to stay connected with the server but I haven't been able to.
I have a cURL request that does exactly what I need but I don't know how to make the same request from Javascript, I am using jQuery Ajax to try and do it.
my cURL code that works fine, looks like this:
curl -s -X PUT -H "Content-Type: application/json" -d '{"data": {"credentials": "MY-HASHED-CREDENTIALS","account_name": "My-account","method": "md5"}}' -k https://my.domain.com:8443/v1/user_auth
And here is the Javascript version that brings back the error:
400 (Bad Request)
$.ajax({ type: "PUT", dataType: "json", url: "https://my.domain.com:8443/v1/user_auth", headers: {"Content-Type" : "application/json"}, data: {"credentials": "MY-HASHED-CREDENTIALS","account_name": "Centrecom","method": "md5"}, success: function(data) { console.log(data); } });
I am open to suggestions on how to get this done in Javascript, either by translating the cURL or by using a library.
-
Hello. i try to click all element of div but it now works
I try clicking on all divs but event works for last divs. how to call function for each div ? i want which div has been clicked the javascript call function for this. enter code here
enter code here
<style> .es{ margin-top: 30px; } .es:hover { cursor: pointer; } </style> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es"> </rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" x="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="100" x="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="100"></rect> <rect height="40" width="80" fill="blue" rx="5" ry="5" class="es" y="200" x="200"></rect> </svg> var svg = document.getElementsByClassName("es"); for (var property in svg) { svg[property].addEventListener("click",function(){ alert("clicked"+svg[property]); }); };
-
Passing more then one value with XMLHttpRequest?
i want to pass more then 1 value in GET request with JS but as i am trying it is not working, function for that is given below,
<script type="text/javascript"> function updatestatus(status, id){ var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","adminfunction.php?status="+status"id="+id,false); xmlhttp.send(null); } </script>
thanks for the help.
-
Cant use exec for specific commands in laravel php
So, I tried to make a command to be executed on my ubuntu linux server.
If I do
systemctl status radio.service
, in anexec
function. That works, but when I try to usesystemctl stop radio.service
, it doesn't do anything. I also can't debug it somehow, what could be the issue?I'm using laravel 5, Ubuntu 16.4 and nginx with php7.
-
explode values using comma with same key in associative array php
I have a output like this-
Array ( [1] => ,5ac907f55c64e,5ab389ea8ec6f [2] => ,5ab389ea8ec6f )
I want to make this output to :
Array ( [1] => 5ac907f55c64e [1] => 5ab389ea8ec6f [2] => 5ab389ea8ec6f )
-
How to set default cursor position in a textbox autofocus is not working
<input type="text" name="VehicalNo" class="form-control" placeholder="Vehicle No" required="required" autofocus>
How to set default cursor position in a textbox autofocus is not working. auto focus attribute is not working -
Slide hidden button
Help me please to make a slide up panel with hidden buttons.
But I have some problem with displacement "Show hidden buttons" after slide up hidden panel.
The code is here:
Caption
<p:outputPanel id="inputPanel1" layout="block" style="width: 600px; height: 120px; padding-top: 50px; border: "> <p:commandButton value="Show Hidden Buttons" widgetVar="saveBtn"> <p:effect event="click" for="inputPanel2" type="slide"> <f:param name="mode" value="'show'" /> <f:param name="direction" value="'down'" /> </p:effect> <p:effect event="blur" for="inputPanel2" type="slide"> <f:param name="mode" value="'hide'" /> <f:param name="direction" value="'down'" /> </p:effect> </p:commandButton> </p:outputPanel>
-
My function doesn't seem to work, yet it looks fine jquery
I am working on a little script, but the script doesn't work because of mysterious reasons or I am missing out on something. I have an input field hidden, when pressed on a button it displays the input field as visible. atleast, this is what it should do, but it doesn't do anything.
$('#E3').append($('<input/>', { 'type': "text", 'name': "extra1", 'id': "extra1", 'class': "hiddenInput" }));
This is the input field that's hidden and should show when clicked on.
$('#AddField').append($('<button/>', { 'type': "button", 'class': "btn btn-primary", 'value': "+", 'id': "AddFieldTakeFunctionEx1" }));
This is the button that should make it work when clicked on
$('#AddFieldTakeFunctionEx1').click(function() { function AddFunctionEx1() { var input = document.getElementById('extra1'); if (!input.style.display || input.style.display == "none") { input.style.display = "block"; } } }); });
this is the script that should make the button work on the input field (when clicked on show, but it won't make it go away. I have 2 buttons, one plus and one minus button. right now im working on the plus button, but as u can see it doesn't work).
Thanks in advance.
-
how to use Orderby in cakephp
I am trying to order data in accordance with its priority... But this query is not working for me.... I cannot figure it out where it went wrong..
please help me to solve this..
$admins = $this->xxx->find()->select($fields)->where($conditions)->contain([ 'yyy' => function ($q) { return $q->autoFields(false) ->select(['id','name','login_url','priority'])->order(['priority' => 'ASC']); } ])->all();
-
Approval and verify button in cakephp
I am new to cakephp.I would one to create an approval and verify buttons which can first verify and then approve.but here the situation is that table A has no verify column('should not add extra cloumn in db').table B has all the functions(verify and approve).In addtion, they verify page button shows the table A datas of a(no changes can be made at the table data).
Thank you in advance
-
Cakephp CSV helper generating leading space
I'm creating a CSV file using CakePHP CSV helper.
I'm always getting one space at the beginning of the column. I have used
trim
,ltrim
but nothing works.I have also added
ob_start
andob_end_clean
but nothing works.My CSV code is
<?php class CsvHelper extends AppHelper { var $delimiter = ','; var $enclosure = '"'; var $filename = 'Export.csv'; var $line = array(); var $buffer; function CsvHelper() { $this->clear(); } function clear() { $this->line = array(); $this->buffer = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+'); } function addField($value) { $this->line[] = $value; } function endRow() { $this->addRow($this->line); $this->line = array(); } function addRow($row) { fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure); } function renderHeaders() { ob_start(); header('Content-Encoding: UTF-16LE'); header('Content-Type: text/csv; charset=UTF-8'); header("Content-type:application/vnd.ms-excel"); header('Content-Disposition: attachment; filename="' . $this->filename . '"'); header("Pragma: no-cache"); header("Expires: 0"); ob_end_clean(); } function setFilename($filename) { $this->filename = $filename; if (strtolower(substr($this->filename, -4)) != '.csv') { $this->filename .= '.csv'; } } function render($outputHeaders = true, $to_encoding = null, $from_encoding = "auto") { if ($outputHeaders) { if (is_string($outputHeaders)) { $this->setFilename(trim($outputHeaders)); } $this->renderHeaders(); } rewind($this->buffer); $output = stream_get_contents($this->buffer); if ($to_encoding) { $output = mb_convert_encoding($output, $to_encoding, $from_encoding); } return $this->output(trim($output)); } } ?>
I'm calling this helper from this ctp file to download and generate report.
<?php $line = $gen_reports[0]['Report']; $arrayKeys = array_keys($line); $this->CSV->addRow($arrayKeys); echo $this->CSV->render("filename");die; ?>
and the result I'm getting always a leading space at the very first column:
How can I get rid of this space?
-
Cross Domain Cookies using img tags
I am currently working on implementing cross domain cookies for two domains that I own and that I can update server side and client code for. My ultimate goal is to create a cookie on example2.com that is analogous to one on example1.com.
I came across this article, which outlines a neat solution but I have a few questions about it. The basic summary from the article is the following:
You render a page on example1.com and make a request via
img
to example2.com (domain missing cookie)<!DOCTYPE html> <html> <head> </head> <body> <img src="http://www.example2.com/setCookie.php" style="display:none;" /> </body> </html>
where setCookie.php on example2.com is:
<? setcookie("MyCookie", "example2.com", time()+3600); ?>
I have tried this in my application and it seems to work (cookie is available on example2.com when I visit it), but I don't understand what's going on in the background at the browser level. The
img
makes a request to example2.com's server and returns a response with proper headers so that a cookie will be set, but this request is coming from example1.com and in the above example we never render the url example2.com in the browser. How does the browser set a cookie for example2.com without ever rendering it client side and only hitting the web server at /setCookie.php? Can cookies be set for domains without being on them at the time? -
Reading a cookie from a parent application in C# web app
Background: I have a web forms application which is the parent app . This app handles user login, and a successful login writes to a cookie.
There are MVC applications which are add-ons to the parent site. I made them in MVC so I didn't have to keep creating web forms. These sub applications read from the cookie set by the parent application to ensure the user is logged into the system.
Recently, I just upgraded to Visual Studio Enterprise 2017 and now, when I am running these applications locally, the sub MVC site is unable to read from the cookie of the parent site.
Both the parent site and the sub sites run on different localhost ports, which was the case before, but I am thinking that might be why the sub site cannot read?
E.g. parent site: locahost:57561 sub site: localhost:53349
My constructor which takes the cookie is:
public SessionManagement(Controller controller) : base(controller.ControllerContext.HttpContext.Request.Cookies["sitename"]) { }
But the Cookies object is empty.
Is there a way to read cookies set by a different localhost and port, and indeed a domain once this is published?
Is there a way to have 2 apps running on the same port?
-
Ruby on Rails simple dropdown does not retain value on a page refresh using cookies
I have this very simple dropdown that selects the currency I want my page to display in. However, whenever I change the currency and then refresh the page, the selected option changes back to USD (the default value). I attempted to use cookies, but it is not working.
Here is my HTML which is just a form that calls a method in my controller:
<%= form_tag "/cryptos/currency_selector" do %> <%= select_tag(cookies[:currency_symbol], options_for_select([['USD'], ['JPY'], ['AUD'], ['EUR'], ['GBP'], ['CHF']], params[:currency_symbol]), :onchange => "this.form.submit();") %> <% end %>
Controller Code which just prints out cookie value for testing:
#handles currency conversion dropdown selector def currency_selector puts cookies[:currency_symbol] end
It's not even setting my cookie since I get an empty string in the terminal. Any help would be greatly appreciated!