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 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

$(document).ready(function() {
  if (document.cookie.indexOf("runOnce") < 0) {
      modal: true,

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


    // Hide the close button

    //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

    // 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.");

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

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

$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.

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
peter-cooperAuthor Commented:
Thank you for your help. Got me started on the right path.
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

From novice to tech pro — start learning today.