0

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?

BrandN00b
  • 63
  • 1
  • 8
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Anonymous Apr 24 '14 at 21:53
  • 2
    It looks like you're missing semi-colons after the `echo` statements – Martin Tournoij Apr 24 '14 at 21:55
  • That "duplicate" is certainly the Mother-of-all-PHP-Error-Questions. However, it does not mention either error message (no mention of T_ELSE or T_STRING). This is not a duplicate of that question. – Eric J. Apr 24 '14 at 21:58
  • Not sure why you're getting an error in the first example, but I'd go with the second form -- much easier to read. Once you correct the issue identified by @Carpetsmoker, you should be good to go. – Kryten Apr 24 '14 at 21:59
  • I think the problem of the first example is the `?>` – Adrian Preuss Apr 24 '14 at 22:04

2 Answers2

3

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>";
Jerry
  • 1,141
  • 1
  • 13
  • 18
3

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>";
    }
?>
Adrian Preuss
  • 3,228
  • 1
  • 23
  • 43