Cookie and Browser Detection

He has: 1,380 posts

Joined: Feb 2002

Hey...

I am trying to do a few things:
-if they have a cookie set, read that cookie, and redirect based on it's value
-if they don't have a cookie set:
-detect browser resolution and set cookie to tell that value
-and if it doesn't automatically do that, have them click on their resolution to set a cookie and redirect

Cookie is setting, but I'm having a problem reading it. I posted awhile ago about reading a cookie, and I followed examples, but no matter what I try, it doesn't work.

Here's the main page's code:

<?php
echo \"xml version=\\"1.0\\" encoding=\\"UTF-8\\"\n\";

if (isset(
$_COOKIE['resolution'])) {
   
$cookie = $_COOKIE['resolution'];
    header(\"Location: http://www...com/\".
$cookie.\".php\");
} else {

print (\"&lt;script type=\\"
text\/javascript\\"&gt;
if ((screen.width=640) && (screen.height=480)) {
    var resolution = small;
    }
else {
    if ((screen.width=800) && (screen.height=600)) {
        var resolution = medium;
        }
    else { if ((screen.width=1024) && (screen.height=7680)) {
        var resolution = norm;
            };
        };
<\/script>\");

if (
$resolution == \"small\") {
   
$value = \"small\";
   
$path = \"/\";
   
$domain = \"http://www...com\";
    setcookie(\"resolution\",
$value, $path, $domain);
    header (\"Location: http://www.../smindex.php\");
    }
elseif (
$resolution == \"medium\") {
   
$value = \"medium\";
   
$path = \"/\";
   
$domain = \"http://www...com\";
    setcookie(\"resolution\",
$value, $path, $domain);
    header (\"Location: http://www.../medindex.php\");
    }
elseif (
$resolution == \"norm\") {
   
$value = \"normal\";
   
$path = \"/\";
   
$domain = \"http://...com\";
    setcookie(\"resolution\",
$value, $path, $domain);
    header (\"Location: http://www.../normindex.php\");
    };
};
?>

followed by
  <td><a href="http:/.../php/setsm.php" alt="[640x480]">640x480</a>
</td>
<td><a href="http://.../php/setmed.php" alt="[800x600]">800x600</a>
</td>
<td><a href="http://.../php/setnorm.php" alt="[1024x768]">1024x768</a>
</td>

when you click on one of those links, it sends you to a php page with contents like:

<?php
$value
= \"normal\";
$path = \"/\";
$domain = \"http://www...com\";

setcookie(\"resolution\",
$value, time()+60*60*24*30, $path, $domain);
header(\"Location: http://.../msc/normindex.php\");
?>

So when you goto the main page (index.php), it reads the cookie but tries to direct you to the URL of http://www...com/.php , whichi makes me think it (obviously) isn't reading right. Any help is appreciated. Thanks.

Chroder's picture

He has: 91 posts

Joined: Mar 2004

Your trying to use Javascript variables within your PHP script, I don't think it works that way. Try getting the value from Javascript, then redirecting the user to the same page but with the info in a query string.

He has: 1,380 posts

Joined: Feb 2002

#1- I made a post: http://webmaster-forums.net/showthread.php?t=23879
follow the links
#2- Forget the javascript even...the PHP part doesn't work (opening cookie and redirecting)

Chroder's picture

He has: 91 posts

Joined: Mar 2004

I'd do something like this. Not tested, but works in theory Wink

<?php
// cookie already set.
// might want to check the value though, somebody might get a
// corrupt cookie and then will see a \"does not exist\" or something
if(isset($_COOKIE['resolution']))
{
   
$res = $_COOKIE['resolution'];
   
header(\"Location: http://www...com/\".$res.\".php\");
}

// JS has detected the resolution. time to set the cookie and redirect
elseif(isset(
$_GET['resolution']))
{
   
$resolution = $_GET['resolution'];

    // small
    if (
$resolution == \"small\")
    {
       
$value = \"small\";
       
$path = \"/\";
       
$domain = \"http://www...com\";
        setcookie(\"resolution\",
$value, $path, $domain);
        header (\"Location: http://www.../smindex.php\");
    }

    // medium
    elseif (
$resolution == \"medium\")\\
    {
       
$value = \"medium\";
       
$path = \"/\";
       
$domain = \"http://www...com\";
        setcookie(\"resolution\",
$value, $path, $domain);
        header (\"Location: http://www.../medindex.php\");
    }

    // everything else is normal. just incase some user puts some wacky number in
    else
    {
       
$value = \"normal\";
       
$path = \"/\";
       
$domain = \"http://...com\";
        setcookie(\"resolution\",
$value, $path, $domain);
        header (\"Location: http://www.../normindex.php\");
    }
}

// Detect res
else
{
    echo \"xml version=\\"
1.0\\" encoding=\\"UTF-8\\"\n\";
   
    &lt;script language=\"javascript\" type=\"text/javascript\"&gt;

    if((screen.width = 640) && (screenheight = 480))
        var resolution = 'small';
    else if((screen.width = 800) && (screen.height = 600))
        var resolution = 'medium';
    else if((screen.width >= 1024 && (screen.height >= 768))
        var resolution = 'norm';
    else
        var resolution = 'small';

    window.location = \"=
$_SERVER['PHP_SELF']?resolution=\" + resolution;
   
    &lt;/script&gt;
   
}
?>

He has: 1,380 posts

Joined: Feb 2002

All well and good, but you don't seem to understand. You have the EXACT same cookie detection as I do...but yet when you detect the cookie, it goes to http://www.mywebsite.com/.php when it should be http://www.mywebsite.com/whatever.php

Chroder's picture

He has: 91 posts

Joined: Mar 2004

Eskater05 wrote: it goes to http://www.mywebsite.com/.php when it should be http://www.mywebsite.com/whatever.php

Thats because you were setting the cookie to a value of "", or nothing. You need to let PHP have a way of obtaining the value.

Chroder's picture

He has: 91 posts

Joined: Mar 2004

The cookie detection works fine, for me anyway. The only problem with my alteration is its not redirecting the user with the query string so it can get the resolution in PHP. Try adding "?resolution=small" to the end of the URL of the code I gave you, it works. Just need to find why the javascript redirection isn't working...

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

You shouldn't name variables the same as name as the $_VARIABLE -- it's just going to cause problems. Also, you don't need to do the concatenation bit, the $variable is within the double quotes and will be parsed.

<?php
if (isset($_COOKIE['resolution'])) {
   
$resolution = $_COOKIE['resolution'];
   
header(\"Location: http://www.yoururl.com/$resolution.php\");
} else {
   
$domain = '.yourdomain.com';
   
$path = '/optional/directory/';

    // you'd get this from wherever
   
$resolution = 'huge';
   
    // if you're consistent, you can do this:
    //
$value = $resolution;
    // and leave the
$value line out of each case

    switch(
$resolution) {
        case 'small':
           
$value = 'small';
           
$page = 'smindex.php';
            break;
        case 'medium':
           
$value = 'medium';
           
$page = 'medindex.php';
            break;
        case 'norm':
           
$value = 'norm';
           
$page = 'normindex.php';
            break;
        case 'huge':
           
$value = 'huge';
           
$page = 'hugeindex.php';
            break;
        case 'undefined':
           
$value = 'medium';
           
$page = 'medindex.php';
            break;
        default:
           
$value = 'norm';
           
$page = 'normindex.php';
            break;
    }
    setcookie('resolution',
$value, time()+3600*24*365,$path,$domain,0);    
    header (\"Location:
$domain/$path/$page\");
}
?>

If you leave off the JavaScript, can you get it to work?

Also, your JavaScript isn't working right:

&lt;script type="text/javascript"&gt;
if (screen.width<=640) {
// if it's less than or equal to 640 pixels wide, it's small
var resolution = 'small'
}
else if (screen.width>640 && screen.width<=800) {
// if it's more than 640, but less than or equal to 800 wide, it's medium
var resolution = 'medium'
}
else if (screen.width>800 && screen.width<=1024) {
// if it's more than 800, but less than or equal to 1024, it's normal
var resolution = 'norm'
}
else if (screen.width>1024) {
var resolution = 'huge'
}
else {
var resolution = 'undefined'
}
&lt;/script&gt;
'

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I should point out that while the code I gave you will work, it won't give you want you want because you're not storing the page names in cookie...

You'll have to set up another switch($resolution) piece to send folks to the right code. Or move things around. Anyway, that should get you closer to where you want to be.

Chroder's picture

He has: 91 posts

Joined: Mar 2004

I think its easier to set the cookie within Javascript.

<?php
if(isset($_COOKIE['page']))
{
   
header(\"Location: http://yourdomain.com/{$_COOKIE['page']}\");
}

else
{
    echo \"xml version=\\"
1.0\\" encoding=\\"UTF-8\\"\n\";
   
    <html>
    <head>
    &lt;script language=\"javascript\" type=\"text/javascript\"&gt;
        if(screen.width <= 640)
            var resolution = 'small';
        else if(screen.width > 640 && screen.width <= 800)
            var resolution = 'medium';
        else if(screen.width > 800 && screen.width >= 1024)
            var resolution = 'norm';
        else
            var resolution = 'small';
            // if no other value is set, use this default. you could just get rid
            // of the \"small\" if above, but i wanted it easy to change, so here it is again :p

        var page;

        switch(resolution)
        {
            case 'small':
                page = 'small.php';
                break;
            case 'medium':
                page = 'medium.php';
                break;
            default:
            case 'norm':
                page = 'norm.php';
                break;
        }

        var cookieDate = new Date(\"January 1, 2023\");
        var cookieExpire = cookieDate.toGMTString();

        document.cookie = \"page=\" + page + \";expires=\" + cookieExpire;
        window.location.reload(false);
    &lt;/script&gt;
    </head>
    <body>
    &nbsp;
    </body>
    </html>
   
}
?>

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I agree it's easier to set the cookie in JavaScript and access it otherwise. Smiling

Chroder's picture

He has: 91 posts

Joined: Mar 2004

But if the user doesn't have javascript enabled, the whole idea is b0rked. I'd do it via a query string. The javascript would automatically reload the page if the resolution was found, but text links/buttons/whatever would be displayed if it didn't. But thats me. Sticking out tongue

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Well, technically I wouldn't use resolution based designs, lol... Wink

Chroder's picture

He has: 91 posts

Joined: Mar 2004

lol You and your fancy CSS XHTML Strict valid designs! Sticking out tongue

He has: 1,380 posts

Joined: Feb 2002

Well "fudge" it all, because I got a change in design from the client.

Haha...well, thanks anyways.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.