Domanda

I'm trying to display a "log in" / "log out" link. I want to display the log in link if they are not logged in, etc. I'm having trouble getting this to work. Heres what I've been trying:

<?php if (isset($_SESSION['access_token'])) 
 { ?> <a href="logout.php">Log out</a><?php } ?>
<?php
else 
{ ?> <a href="login.php">Log In</a>  <?php } ?>

Basically, if the assess_token is set, it will show a link to log out. And if not, a log in link. I keep getting this as an error:

Parse error: syntax error, unexpected T_ELSE

And i've tried this variation:

<?php if (isset($_SESSION['access_token'])) 
{ echo "<a href="logout.php">Log out</a>" }
else 
{ echo "<a href="login.p">Log In</a>" } ?>

which get me:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

what am i missing here?

È stato utile?

Soluzione

You have 2 examples and both have syntax errors:

Parse error: syntax error, unexpected T_ELSE

If you format your code correctly, you found the error:

<?php
    if (isset($_SESSION['access_token'])) {
        ?> <a href="logout.php">Log out</a><?php
    } else {
        ?> <a href="login.php">Log In</a> <?php
    }
?>

That:

<?php } ?>
<?php
else 
{ ?>

is not valid. The PHP intepreter can't handle this! Please use <?php } else { ?>

The second: Use an Syntax-Highlighted editor! You see that the Syntax is not correct. Escape the " in your code, you forgotten the ; after the echo:

<?php
    if(isset($_SESSION['access_token'])) {
        echo "<a href=\"logout.php\">Log out</a>";
    } else {
        echo "<a href=\"login.php\">Log In</a>";
    }
?>

Altri suggerimenti

It's normal . look at your echo . There are only double quotes. You have to use (for example) double quotes for echo, and single quotes to delimit logout.php and login.php

echo "<a href='logout.php'>Log out</a>";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top