Search code examples
sessionif-statementcookiesechohref

Can you fix my PHP If Statement, using a Href?


I am using an if statement and trying to get one of two statements to show, depending on the session running. please help. thank you.

    if (session_name== "admin"){
    echo ("<a href="admin/logout.php">[Admin Logout]</a>");
    }else {
         echo "<a href="logout.php">[Member Logout]</a>";   
    }
    ?>

Also there is 2 syntax errors on the echo parts.


Solution

  • You if you use double quotes around the argument to echo, you should use single quotes inside it for the HTML attribute, or vice versa (an alternative is to escape the inner quotes with backslash, but I never understand why people prefer this). And you need $ before the variable name.

    if ($session_name== "admin"){
        echo ("<a href='admin/logout.php'>[Admin Logout]</a>");
    }else {
         echo "<a href='logout.php'>[Member Logout]</a>";   
    }