简体   繁体   English

继续执行PHP代码之前的用户操作

[英]User action before continue a PHP code

I am using the below php code to create and store a cookie. 我正在使用以下php代码创建和存储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. 在继续创建和保存Cookie之前,我如何才能获得是或否的用户操作。

I am using Codeigniter 我正在使用Codeigniter

You can't interact with the user from within a PHP script. 您无法通过PHP脚本与用户进行交互。 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 ),然后向用户显示一个页面,供他们选择是或否。 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. 请注意, if有人打开多个选项卡,然后在所有选项卡上选择“是”,则获得第二个页面上的“ if ”,将获得比实际应有的更多“插槽”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM