简体   繁体   中英

Set cookie by php session username

I have set a general cookie that works fine for a run once situation. However, what I need to do is set a cookie by username so a message is displayed only once per user. I am using php to set a session for the username linked to mysql.

I have posted the code I am using for the general cookie and wonder if it can be modified to target a username rather than a general cookie.

I would be grateful if someone could help with this. Thanks

<script>
$(document).ready(function() {
  if (document.cookie.indexOf("runOnce") < 0) {
    $("#message3").dialog({
      modal: true,

      buttons: {
        'Confirm': function() {
            document.cookie = 'runOnce = true; expires = Fri, 31 Dec 9999 23:59:59 GMT; path = /';
            $(this).dialog("close");
          }
          /* ,
          No: function () {
          $( this ).dialog( "close" ); 
          }*/

      }
    });

    // Hide the close button
    jQuery("a.ui-dialog-titlebar-close").hide();

    //Disable the confirm button on page load
    $(":button:contains('Confirm')").attr("disabled", true).addClass("ui-state-disabled");

    // Uncheck the 'agree' checkbox
    $(function() {
      $('#message3 input[type=checkbox]').attr('checked', false);
    });

    // Hide red message paragraph
    $('#agreemsg').hide();

    // Function to enable and disable confirm button based on checkbox click event
    $("#checkme").click(function() {

      var isChecked = $("#checkme").is(":checked");

      if (isChecked) {

        //alert("CheckBox checked.");
        $(":button:contains('Confirm')").removeAttr('disabled').removeClass('ui-state-disabled');
        $('#agreemsg').show();

      } else {
        //alert("CheckBox not checked.");
        $(":button:contains('Confirm')").prop("disabled", true).addClass("ui-state-disabled");
        $('#agreemsg').hide();
      }
    });
    //}
  };
});
</script>

Cookies are not the correct place to implement this. It's best to add additional property in the database associated with your user, for example:

Table: users

Columns:

id
username
... other fields ...
is_first_login
  1. Display your message if is_first_login is not true
  2. Mark is_first_login to true in the database after your message has been displayed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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