With a good start from the folks on this list and a little JavaScript
digging, I found an excellent solution to my problem that I thought I'd share:

Problem: Want to set a cookie then test for it on the same page.  I don't
want to to do page redirection to ensure optimal search engine friendliness.

Solution: Set the cookie in PHP then test for the cookie with JavaScript.
Code example below:

<?
if (! $MySession)
{
        $SessionID = "2001 A Space Odyssey";
        setCookie('MySession',$SessionID,time()+6048000,"/","",0);
}
?>
< h t m l > <!--html tag done that way to ensure non-HTML appearance in e-mail
<head>
<title>Test Page</title>
<SCRIPT LANGUAGE="JavaScript">

cookie_name = "MySession";
var Session;

function getSession()
{
        if(document.cookie)
        {
                index = document.cookie.indexOf(cookie_name);
                if (index != -1)
                {
                        namestart = (document.cookie.indexOf("=", index) + 1);
                        nameend = document.cookie.indexOf(";", index);
                        if (nameend == -1)
                        {
                                nameend = document.cookie.length;
                        }
                        Session = document.cookie.substring(namestart, nameend);
                        return Session;
                }
        }
}

Session=getSession();
if (!Session)
{
        CookieMessage = "Cookies are not enabled.  You can browse the site, but
will need to enable them to be able make a purchase."
}
else
{
        CookieMessage = ""
}
</SCRIPT> 
</head>
<body>
<SCRIPT LANGUAGE="javascript">
document.write(CookieMessage);
</SCRIPT>

blah, blah, blah, the rest of the web page...

Enjoy,
Jeff Gannaway

At 03:36 PM 7/5/01 -0500, Jeff Gannaway wrote:
>OK, I'm working on a site that absolutely must use a cookie containing a
>session identifier.
>
>I know how to set a cookie through PHP, but I need to do something else too.
>
>The same page MUST identifier whether the cookie was accepted or not.  I've
>seen sites like NetFlix.com that will test to see if their cookie was
>accepted, and if not, print up a page that says "Sorry, you've got have
>your Cookies on or go somewhere else."
>
>Thanks,
>Jeff Gannaway


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to