WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Run a function once per session.
Categories : Beginner Guides, Java Script, Cookies Click here to Update Your Picture
Michael Regan
Date : Dec 17th 2005
Grade : 3 of 5 (graded 3 times)
Viewed : 5894
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Michael Regan
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

The Problem:
The client had a popup window for news and announcements that displayed each time the index page was loaded. The client wanted to the popup to only be displayed the first time the index page was called.

The Solution:
I wrote a Javascript that tested for a cookie each time the page was loaded. If the cookie had been set nothing happens. If the cookie is not set the code sets the cookie and displays the popup.

Here is the code:
// ++++++++++++++++++++++++++++++++++++++++++
// Run Once Per Session
//
// Replace the alerts by functions that need to
// be run once per session.
//
// Written by: Michael Regan
// Website   : www.owt4nowt.ca
//
// Released under the GPL.
// ++++++++++++++++++++++++++++++++++++++++++
var key_value = "myTestCookie=true";
var foundCookie = 0;

// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');

    // Walk through the array
    for(var i=0;i < cookieArray.length;i++)
        {
               var checkCookie = cookieArray[i];
        // Remove any leading spaces
               while (checkCookie.charAt(0)==' ')
               {
                 checkCookie = checkCookie.substring(1,checkCookie.length);
               }
       
        // Look for cookie set by key_value
                if (checkCookie.indexOf(key_value) == 0)
               {
                  alert("Found Cookie ");
            // The cookie was found so set the variable
                   foundCookie = 1;
               }
    }
    // Check if a cookie has been found
    if ( foundCookie == 0)
    {
        // The key_value cookie was not found so set it now
        document.cookie = key_value;
        alert("Setting Cookie");
    } 



Store, retrieve and delete cookies using JavaScript.
Categories : Java Script, Cookies, Beginner Guides, Cookies
Simple Javascript CSS Digital Clock
Categories : Java Script, Date Time, CSS, Beginner Guides, Web Design
A very simple PHP single password cookie based login without usernames.
Categories : PHP, Cookies, Security, Beginner Guides
Validating a URL with JavaScript RegExp
Categories : Java Script, Data Validation, Beginner Guides
Easy alert box pop-up function
Categories : PHP, Java Script, Beginner Guides
Show hide table rows to make dynamic forms
Categories : Beginner Guides, Java Script, Form Processing, HTTP
Ajax - Perform a simple server side request and update the current HTML page.
Categories : AJAX, Java Script, Beginner Guides
Form Processing : with alert Highlight field name which is not filled by user
Categories : Java Script, Form Processing, Data Validation, Beginner Guides, Web Design
Local Time clock and Server time usign PHP and JavaScript
Categories : PHP, Java Script, Date Time, Beginner Guides
Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP
Conditional Check - a script that allows a user to submit a form only if the user check a checkbox.
Categories : HTML, Java Script, Form Processing, Beginner Guides
PHP and javascript mouseover, mouseout, and mousedown events
Categories : PHP, Java Script, Form Processing, Beginner Guides
Simple Cookie example
Categories : PHP, Beginner Guides, Cookies
Get your browser details using javascript
Categories : Java Script, Beginner Guides, Browsers
Changing the Style of form objects using the JavaScript OnClick method.
Categories : Java Script, Form Processing, Beginner Guides, CSS