7. Modify your authentication process to implement the notion of a session. More
ID: 3860413 • Letter: 7
Question
7. Modify your authentication process to implement the notion of a session. More explicitly, you should be able to create a session upon successful login and destroy the session after the user logs out (see examples 12.5 and 12.8 in Nixon's book). 8. Test your implementation of a session by conditioning the access to the wall page to successfully logged in users only. If a user is not logged in and tries to access the wall they should be redirected to the login page (see example 12.6 in Nixon's book). 9. IMPORTANT: NAMING CONVENTIONS o Name your main file (with login/signup screen) index.php o Name your wall file (which should only be accessible to registered users) wall.php o Make both files available at your p7 folder
<?php
session_start();
require_once './php/db_connect.php';
$con = mysqli_connect("localhost","root","","phppot_examples");
$mssg="";
if(!empty($_POST["logged"])) {
$res = mysqli_query($con,"SELECT * FROM users WHERE uname='" . $_POST["uname"] . "' and pswd = '". $_POST["pswd"]."'");
$ro = mysqli_fetch_array($res);
if(is_array($ro)) {
$_SESSION["user_id"] = $ro['user_id'];
} else {
$mssg = "Invalid Username or Password!";
}
}
if(!empty($_POST["logout"])) {
$_SESSION["user_id"] = "";
session_destroy();
}
?>
<html>
<head>
<title>User Login</title>
<style>
#frmLogin {
padding: 20px 60px;
background: #B6E0FF;
color: #555;
display: inline-block;
border-radius: 4px;
}
.fldGrp {
margin:15px 0px;
}
.div11 {
padding: 8px;width: 200px;
border: #A3C3E7 1px solid;
border-radius: 4px;
}
.submitBtn {
background: #65C370;
border: 0;
padding: 8px 20px;
border-radius: 4px;
color: #FFF;
text-transform: uppercase;
}
.memDash {
padding: 40px;
background: #D2EDD5;
color: #555;
border-radius: 4px;
display: inline-block;
text-align:center;
}
.outBtn {
color: #09F;
text-decoration: none;
background: none;
border: none;
padding: 0px;
cursor: pointer;
}
.errMssg {
text-align:center;
color:#FF0000;
}
.demo-content label{
width:auto;
}
</style>
</head>
<body>
<div>
<div>
<?php if(empty($_SESSION["user_id"])) { ?>
<form action="" method="post" id="frmLogin">
<div class="errMssg"><?php if(isset($mssg)) { echo $mssg; } ?></div>
<div class="fldGrp">
<div><label for="logged">Username</label></div>
<div><input name="uname" type="text" class="div11"></div>
</div>
<div class="fldGrp">
<div><label for="pswd">Password</label></div>
<div><input name="pswd" type="pswd" class="div11"> </div>
</div>
<div class="fldGrp">
<div><input type="submit" name="logged" value="Login" class="submitBtn"></span></div>
</div>
</form>
<?php
} else {
$res = mysqlI_query($con,"SELECT * FROM users WHERE user_id='" . $_SESSION["user_id"] . "'");
$ro = mysqli_fetch_array($res);
?>
<form action="" method="post" id="logout">
<div class="memDash">Welcome <?php echo ucwords($ro['display_name']); ?>, You have successfully logged in!<br>
Click to <input type="submit" name="logout" value="Logout" class="outBtn">.</div>
</form>
</div>
</div>
?>
</body>
</html>
Explanation / Answer
<?php
session_start();
require_once './php/db_connect.php';
$con = mysqli_connect("localhost","root","","phppot_examples");
$mssg="";
if(!empty($_POST["logged"])) {
$res = mysqli_query($con,"SELECT * FROM users WHERE uname='" . $_POST["uname"] . "' and pswd = '". $_POST["pswd"]."'");
$ro = mysqli_fetch_array($res);
if(is_array($ro)) {
$_SESSION["user_id"] = $ro['user_id'];
} else {
$mssg = "Invalid Username or Password!";
}
}
if(!empty($_POST["logout"])) {
$_SESSION["user_id"] = "";
session_destroy();
}
?>
<html>
<head>
<title>User Login</title>
<style>
#frmLogin {
padding: 20px 60px;
background: #B6E0FF;
color: #555;
display: inline-block;
border-radius: 4px;
}
.fldGrp {
margin:15px 0px;
}
.div11 {
padding: 8px;width: 200px;
border: #A3C3E7 1px solid;
border-radius: 4px;
}
.submitBtn {
background: #65C370;
border: 0;
padding: 8px 20px;
border-radius: 4px;
color: #FFF;
text-transform: uppercase;
}
.memDash {
padding: 40px;
background: #D2EDD5;
color: #555;
border-radius: 4px;
display: inline-block;
text-align:center;
}
.outBtn {
color: #09F;
text-decoration: none;
background: none;
border: none;
padding: 0px;
cursor: pointer;
}
.errMssg {
text-align:center;
color:#FF0000;
}
.demo-content label{
width:auto;
}
</style>
</head>
<body>
<div>
<div>
<?php if(empty($_SESSION["user_id"])) { ?>
<form action="" method="post" id="frmLogin">
<div class="errMssg"><?php if(isset($mssg)) { echo $mssg; } ?></div>
<div class="fldGrp">
<div><label for="logged">Username</label></div>
<div><input name="uname" type="text" class="div11"></div>
</div>
<div class="fldGrp">
<div><label for="pswd">Password</label></div>
<div><input name="pswd" type="pswd" class="div11"> </div>
</div>
<div class="fldGrp">
<div><input type="submit" name="logged" value="Login" class="submitBtn"></span></div>
</div>
</form>
<?php
} else {
$res = mysqlI_query($con,"SELECT * FROM users WHERE user_id='" . $_SESSION["user_id"] . "'");
$ro = mysqli_fetch_array($res);
?>
<form action="" method="post" id="logout">
<div class="memDash">Welcome <?php echo ucwords($ro['display_name']); ?>, You have successfully logged in!<br>
Click to <input type="submit" name="logout" value="Logout" class="outBtn">.</div>
</form>
</div>
</div>
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.