Set cookie by php session username

Hello
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 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 am using php to set a session for the username linked to mysql.

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>

Open in new window

peter-cooperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Just set your cookie using php
http://php.net/manual/en/function.setcookie.php

$userName= 'Admin';

setcookie("UserName", $userName);

Open in new window


You will be able to access the cookie with your javascript just as if you set it with javascript.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peter-cooperAuthor Commented:
@Scott thanks for reply. Would you have an example of how I can set a cookie using session username using php rather than javascript. The idea is that a message is viewed once only per session. Thanks
0
peter-cooperAuthor Commented:
Thank you for your help. Got me started on the right path.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.