I have to make a simple site which you can log in and out of, and if the user is logged in they see some features which they otherwise would not. I'm not very good with web development however I have managed to get something together which seems to have worked. I've decided I don't want to redirect the user to another page when logging in and logging out so this has made it a bit harder for me to understand.
I just wondered if I'm going about the session starts and destroy in the right way and if anyone could give me any pointers as to making it better if that's even possible.
<?php
if(isset($_POST['logout'])) {
session_destroy();
}
}
session_start();
if(!isset($_SESSION['username'])) {
if (!empty($_POST['username']) && !empty($_POST['password'])) {
$result = mysql_query("SELECT * FROM users WHERE username ='$_POST['username']' AND password = '$_POST['password']'");
if(mysql_num_rows($result))
$_SESSION['username'] = $_POST['username'];
}
else {
echo "";
}
}
}
?>
<?php if(!isset($_SESSION['username'])) {
echo '<div id = "account">
<form name="input" action="index.php" method="post">
Username:<input type="text" name="username" /> Password:<input type="password" name="password" />
<input type="submit" value="GO!" />
</form>
}
else {
echo "Signed in"
<form name='logout' action='index.php' method='post'>
<input type='submit'value='Reset' name='logout'/>
";
} ?>
<?php
$test = mysql_query("SELECT * FROM posts ORDER BY post_id DESC");
if($test) {
while($row = mysql_fetch_array($test)) {
echo '<div class="posts">';
echo "$row[post]";
echo '</div>';
}
}