Jump to content

Remove Form, When It's Been Used.


jepperask

Recommended Posts

Hi guys - I've had problems with this for a couple of hours now, so now im asking you! ;-)

 

I'm creating a blog, where i can log in in the header.php file (which i include on all the pages, as a sticky "move-around" -toolbar.

But I would like the form to be removed when I am logged in, and instead have a log out button and preferences (or just to be removed as a start). I've tried to solve it by adding !$_session['user_id']{*FORM-CODE-WITH-INPUT-AREA-HERE}; meaning, that if the user is logged in, the above thing wont even come up. But i couldn't really make it work. Heres my code:

<?php
session_start();
if(isset($_POST['submit'])) {
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
//include database connection
include 'db_connect.php';

if(empty($user) || empty($pwrd)){
echo("Missing Information");
}else{
$user = strip_tags($user);
$user = $db->real_escape_string($user);
$pwrd = strip_tags($pwrd);
$pwrd = $db->real_escape_string($pwrd);
$pwrd = md5($pwrd);
$query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'");
if($query->num_rows == 1){
while($row = $query->fetch_object()){
$_SESSION['user_id'] = $row->user_id;
}
header('Location: admin-page.php');
exit();
}else{
echo "Missing Information!";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div id="container">
<form action="login.php" method="post">
<p>
<label>Username</label><input type="text" name="username">
<label>Password</label><input type="password" name="pwrd">
<input type="submit" name="submit" value="LogIn">
</p>
</form>

</div>
</body>
</html>

And the code is by the way just the login.php, which i already included in the header.php.

Edited by jepperask
Link to comment
Share on other sites

Stick it in your html...

 

<?php if(isset($_SESSION['user_id'])) { ?>
Welcome | Log Out
<?php } else { ?>
<form action="login.php" method="post">
<p>
<label>Username</label><input type="text" name="username">
<label>Password</label><input type="password" name="pwrd">
<input type="submit" name="submit" value="LogIn">
</p>
</form>
<?php } ?>

 

 

That's just quick and dirty but hopefully you get the drift.

Edited by akphidelt2007
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.