I am trying to make a VERY BASIC login form to test out different PHP things and to practice PHP (I'm only learning it now). Why don't this code work for a login form? I can get the first part to work (the form) but then the php script that processes it won't. Please help me out. The first file. index.php <html> <body> <?php if(isset($_SESSION["login"]) && ($_SESSION["login"]=="true"))   echo "You are Logged In"; else   echo "You are not logged in. Please login Below"; ?> <br> <br> <br> <form action="login.php" method="post"> Password: <input type="password" name="pass"> <br> <input type="submit" value="submit"> </form> </body> </html> PHP: The second file. login.php <?php session_start(); if($_POST["pass"]=="lol")   {   echo "Login Correct";   $_SESSION["login"]="true";   header("Location: index.php);   } else   {   echo "Login Incorrect";   session_destroy();   header("Location: index.php");   } ?> PHP: Any help would be greatly appreciated.
Oh, but you missed out braces around the if {} and the else {}: if(isset($_SESSION["login"]) && ($_SESSION["login"]=="true")) { echo "You are Logged In"; } else { echo "You are not logged in. Please login Below"; } PHP: I didn't test this, but I THINK from personal experience (ahem) that without the braces it will evaluate both the IF and the ELSE and print both messages? It will do SOMETHING wrong, I am pretty sure!
<html> <body> <?php if(isset($_SESSION["login"]) && $_SESSION["login"]=="true"){ echo "You are Logged In"; } else { echo "You are not logged in. Please login Below"; } ?> <br> <br> <br> <form action="login.php" method="post"> Password: <input type="password" name="pass"> <br> <input type="submit" value="submit"> </form> </body> </html> PHP:
You're missing a closing " in the header call in login.php You need to add session_start to index.php (and any other page you want to get access to the $_SESSION variables Use braces for your if statements in index.php
<?php session_start(); ?> <html> <body> <?php if(isset($_SESSION["login"]) && $_SESSION["login"]=="true"){ echo "You are Logged In"; } else { echo "You are not logged in. Please login Below"; } ?> <br> <br> <br> <form action="login.php" method="post"> Password: <input type="password" name="pass"> <br> <input type="submit" value="submit"> </form> </body> </html> Code (markup): You forget to put session_start();
I stand corrected, braces {} are not necessary in an if else if there is just one expression to evaluate - I was reading the PHP manual as a bedtime story last night and came across that BTW, echoing stuff straight out like you are doing is sloppy - you should really look into implementing a Controller/View structure that separates your code from your output as much as possible. A framework like CodeIgniter will really help with this, and with making coding easier too! I know, I used to code like this too I am a reformed character now!
Please let him understand the problem before you introduce some framework. CodeIgniter is too advance for him. How can he understand a fully object oriented framework if he even don't know how to solve this simple problem.
Well, that's one argument, the other argument is that it's better to learn good programming habits from day one. Personally I'm glad I DID learn a lot of pure PHP to begin with, but it's hard to break bad habits later. It's true though, I guess a framework would be hard to understand if you didn't know much pure PHP...