poff
poff

Reputation: 1640

PHP if-statement being 'ignored', rendering both options

So I am extremely new to PHP and am probably making a stupid mistake. I have searched for quite a while, though, and have not been able to figure out what is going wrong.

Using XAMPP all of the PHP works fine. When I load it onto my web server, however, the if statement regulating the log in form displays both the 'log in' form and the 'log out' form at the same time. Obviously not what I want!

Here is the code snippet:

<?php
   if ( isset( $_SESSION['email'] ) ) { // Check to see if user is logged in.  If so, display log out button.
?>
    <form class="navbar-form pull-right" action="logout.php" method="POST">
    <button name="submit" type="submit" class="btn btn-success">Log Out</button>
    </form>
<?php 
   } else { // display log in form
?>
    <form class="navbar-form pull-right" action="login.php" method="POST">
    <input name="email" type="text" class="span2" placeholder="email">
    <input name="password" type="password" class="span2" placeholder="password">
    <button name="submit" type="submit" class="btn btn-success">Log In</button>
    </form>
<?php 
   } 
?>

This is contained in a file index.php

The basic logic is test to see if an email variable has been created for the session (done in a login.php execution) to check if the user is logged in.

I have no idea if this is the best way to create a log in, but that's what I've tried to do.

Again, I am very new to this, so any insight on where to look next would be greatly appreciated!

Thank you!

Upvotes: 1

Views: 1478

Answers (4)

user2758781
user2758781

Reputation:

You didn't use echo. Is it OK to try displying text without an echo statement before?

Upvotes: 0

Arthur Grishin
Arthur Grishin

Reputation: 358

I think your *.php files does not behave like PHP files. You should check your web server settings.

Upvotes: 0

Sree
Sree

Reputation: 971

Try

if(!empty( $_SESSION['email'] ))

and I hope you did session_start() at the beginning.

Upvotes: 0

Yermo Lamers
Yermo Lamers

Reputation: 1951

Do a view source and make sure your PHP code is actually being interpreted. If you see your PHP tags in the source of the browser then your web server is not configured properly.

Upvotes: 5

Related Questions