Reputation: 1453
I am attempting to learn php and I am at a really early stage!
What I am trying to do is hide the div that contains a login form when the user is logged in, I have managed to successfully create the database connection and login form etc in order to allow a user to stay logged in and a session to stay open but not sure the next step to hide the .
I know I need an if statement but not sure where to go!
I wont include the whole code as the html is enormous but here is the form I want to hide:
<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>
not sure what elso you would need to see but here is the php in the page:
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
That is before the tags then before the form I have:
<?php
if ($errorcode == 1)
{
echo "<h3>Your login has failed. Try again</h3>";
}
?>
Would you need the login check php code and the database connection php code to help advise?
Upvotes: 0
Views: 421
Reputation: 95308
Say you have a variable called $logged_in
. Then you could enable that form depending on whether the user is logged in or not like this. Using the special control syntax for templates:
<?php if (!$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>
<?php endif; ?>
As for your PHP code, I'm not sure what the meaning of that $_GET['showerror']
is, but it seems to me like you are somehow performing the authentication in Javascript and then just redirecting to your PHP or something. Note that one could easily just call your script like logincheck.php?errorcode=0
and fool it into thinking that one just logged in correctly.
You should perform the authentication inside your PHP code. If login was successful, just create a session and mark it as authenticated in the $_SESSION
variable, it will then be persistent across page loads.
Upvotes: 4
Reputation: 5174
Remember, PHP basically runs your script, then outputs whatever the result is. (It's a bit more complicated than just that, but that's a topic for another day).
So if you wrap your PHP in an if block, as below, it will only be displayed when appropriate.
<?
if(!$loggedInVariable)//use whatever logic you want here,
//the key is that this should return false if you're 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>
<?php
}
?>
Upvotes: 1