Sven Elmendorp
Sven Elmendorp

Reputation: 39

switch homepage on cookie

In my index PHP file, I have this simple code:

<?php
$enterprisecookie = "enterprise";
$personalcookie = "personal";

switch(!isset($_COOKIE)) {
case $enterprisecookie:
    include 'enterprise.php';
    break;
 case $personalcookie:
    include 'personal.php';
    break;
default:
    include 'portal.php';

}
?>

the idea is simple, when this cookie exist you go to this homepage and if you have none you go to a "portal" which will set the cookie. These are my buttons which sets the cookie.

        <a href="index.php"class="link" 
onClick="SetCookieper('personal','personal','1')"><button class="per"><h1> 
personal</h1>
        <p>texttexttext</p> </a>

        <a href="index.php" class="link" 
onClick="SetCookieent('enterprise','enteprise','1')"><button class="ent">
<h1> 
enterprise</h1>
        <p>textexttext</p>
        </button></a>

         <script>  function SetCookieper(c_name,value,expiredays)
        {
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
        }
        function SetCookieent(c_name,value,expiredays)
        {
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
        }</script>

the cookie sets but the page will still just go to the portal page, any advice?

Upvotes: 0

Views: 74

Answers (2)

kscherrer
kscherrer

Reputation: 5766

You can use one Cookie where you set the mode, lets call it 'pagemode'.

then you can use the switch like this:

switch($_COOKIE['pagemode']){
    case 'enterprise': include 'enterprise.php'; break;
    case 'personal': include 'personal.php'; break;
    default: include 'portal.php';
}

If you really need 2 differently named cookies, use an if-ifelse-else statement:

if(isset($_COOKIE['enterprise'])){
    include 'enterprise.php';
} else if(isset($_COOKIE['personal'])){
    include 'personal.php';
} else {
    include 'portal.php';
}

In case you want to use the first method (one single cookie saving the mode of the homepage), you can set the cookie like this:

<a href="index.php"class="link" 
onClick="SetCookie('pagemode','personal',1)"><button class="per"><h1> 
personal</h1>
        <p>texttexttext</p> </a>

        <a href="index.php" class="link" 
onClick="SetCookie('pagemode','enterprise',1)"><button class="ent">
<h1> 
enterprise</h1>
        <p>textexttext</p>
        </button></a>

<script>  
function SetCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
</script>

Upvotes: 1

Script47
Script47

Reputation: 14540

Your switch check is invalid, you need to pass a parameter for it to check, not the whole $_COOKIE array.

So essentially what you'd have to do is have a the same key for both cookies and compare the values instead of comparing the key name, that is if you wish to use a switch statement.

Or you could just use a simple if elseif else to keep things simpler.

Upvotes: 0

Related Questions