I am using the below php code to create and store a cookie.
if ($logged_in_user['slots'] > $this->devices_model->count_devices($logged_in_user['id'])) {
//HERE SHOULD BE PLACED WHAT I NEED
$cookie = array(
'name' => 'kolass_app',
'value' => md5(uniqid($logged_in_user['id'], true)),
'device' => $this->agent->browser() . ' ' . $this->agent->version(),
'expire' => time() + (10 * 365 * 24 * 60 * 60),
);
$this->input->set_cookie($cookie);
$data['user_id'] = $logged_in_user['id'];
$data['device'] = $cookie['device'];
$data['value'] = $cookie['value'];
$this->devices_model->add_device($data);
redirect(base_url());
}
How can i get a user action yes or no before continuing the creation and saving of the cookie.
I am using Codeigniter
You can't interact with the user from within a PHP script. What you need to do is first perform the first part (the if
), and then show the user a page where they can select yes or no. If they select yes, send them to a page that performs the rest of your code. Beware though that you also need the if
on that second page in case someone opens multiple tabs and then selects yes on all of them, getting more 'slots' than should actually be able to get.
Use
<?php
if ($logged_in_user['slots'] > $this->devices_model->count_devices($logged_in_user['id'])) {
?>
<script>
function myFunction() {
var r = confirm("Are You sure??");
if (r == true) {
<?php
$cookie = array(
'name' => 'kolass_app',
'value' => md5(uniqid($logged_in_user['id'], true)),
'device' => $this->agent->browser() . ' ' . $this->agent->version(),
'expire' => time() + (10 * 365 * 24 * 60 * 60),
);
$this->input->set_cookie($cookie);
$data['user_id'] = $logged_in_user['id'];
$data['device'] = $cookie['device'];
$data['value'] = $cookie['value'];
$this->devices_model->add_device($data);
redirect(base_url())
?>
} else {
alert('Not Served');
}
}
</script>
<?php
}
else
{
echo 'Not Loged In';
}