I asked a very similar question and I approved an answer as I thought it had done the job but it hasnt! for the life of me I can’t understand why this wont work! I want a form in html to dissapear when the user is logged in! Heres my code so far! (I wont display all code as the html is loooong but I think this is relevent, please bear in mind I am at the early beginner stage of php):
I have this at the top of my page:
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
Then the code to try and hide the form and to display an error if the username/password is incorrect:
<?php
if ($errorcode == 1)
{
echo "<h3>Your login has failed. Try again</h3>";
}
?>
<?php
session_start();
if (!isset($_SESSION['is_logged_in'])) {
?>
<form id="login" name="login" action="logincheck.php" method="post">
User Name:
<input type="text" name="username"></input>
Password:
<input type="password" name="password"></input>
<input type="submit" name="submit" value="login"></input>
<a href="javascript: void(0)" onclick="popup('register.html')" > (Register)</a>
<br /><br /><br />
</form>
<?
}
?>
The next code is what I wrote to check if the login credentials are correct, and to turn us back to the index.html page, I assign a value to errorcode so I can display the error message if the credentials are wrong and I assign a value to is_logged_in to hide the form if they are correct. In both instances we are turned back to index.html but hopefully either with a form on display and an eroor message, or no form and no message: (dbconn.php is my connection to the mysql database)
<?php
session_start();
require "dbconn.php";
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM person WHERE name ='".$username."' AND password='".$password."'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array( $result );
if ($row != null)
{
$_SESSION['username'] = $row['name'];
header("Location: index.html?is_logged_in=1?");
exit();
}
else
{
header("Location: index.html?showerror=1");
exit();
}
?>
but the problem is that neither of the two outcomes, I get no error message and I get no hidden form, because nothing is happening it is difficult for me to gauge where I am going wrong!
Thank you sooooo much for any help… I spent ages setting up xampp properly and got ready to learn lots but I have been stuck here for a good few hours now!
EDIT: still getting to grips with this code editing! didnt display some of code but I think its done now!
You don’t set the session variable is_logged_in, put
into the login file (between the brackets if $row != null)