Gh0st_Rider Gh0st_Rider - 3 months ago 25
HTML Question

Can't get login page to redirect when user credentials are entered

Hi there so I'm having so problem with a little project I've been working on. I'm 17 Years old in high school and trying to learn as much as I can about html, php, MySQL, etc. I created a database with MySQL and I know that I have that setup correctly, because the login page I'm using is able to know what is the right, and wrong user accounts. I cant seem to be able to get the page to redirect to the welcome page on a successful login. Here is my code.

Login Page Code

<?php
include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);

$sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
session_register("myusername");
$_SESSION['username'] = $myusername;

header("location: welcome.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>

<head>
<title>Login Page</title>

<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

label {
font-weight:bold;
width:100px;
font-size:14px;
}

.box {
border:#666666 solid 1px;
}
</style>

</head>

<body bgcolor = "#FFFFFF">

<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

<div style = "margin:30px">

<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>

<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

</div>

</div>

</div>

</body>
</html>


Session Code

<?php
include('config.php');
session_start();

$user_check = $_SESSION['myusername'];

$ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");

$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session = $row['username'];

if(!isset($_SESSION['username'])){
header("location:welcome.php");
}
?>


Redirected Page

<?php
include('session.php');
?>
<html>

<head>
<title>Welcome </title>
</head>

<body>
<h1>Welcome <?php echo $login_session; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>

</html>
<?php
session_start();

if(session_destroy()) {
header("Location: login.php");
}
strong text ?>

Answer
 <?php
 session_start();
 include("config.php");


if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);

$sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
    $_SESSION['username'] = $myusername;
    header("Location: welcome.php");
}else {
    $error = "Your Login Name or Password is invalid";
    header("Location: login.php?error=".$error);
}
}
else {

if (isset($_GET['error'])) {
    echo $_GET['error'];
}

?>
<html>

<head>
    <title>Login Page</title>

    <style type="text/css">
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
        }

        label {
            font-weight: bold;
            width: 100px;
            font-size: 14px;
        }

        .box {
            border: #666666 solid 1px;
        }
    </style>

</head>

<body bgcolor="#FFFFFF">

<div align="center">
    <div style="width:300px; border: solid 1px #333333; " align="left">
        <div style="background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

        <div style="margin:30px">

            <form action="" method="post">
                <label>UserName :</label><input type="text" name="username" class="box"/><br/><br/>
                <label>Password :</label><input type="password" name="password" class="box"/><br/><br/>
                <input type="submit" value=" Submit "/><br/>
            </form>

            <div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

        </div>

    </div>

</div>

</body>
</html>
<?php
}
?>

this is Session Code

 <?php
 session_start();
 include('config.php');


 $user_check = $_SESSION['myusername'];

 $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");

 $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

 $login_session = $row['username'];

 ?>

Redirected Page

 <?php
 include('session.php');
 ?>
<html>
 <head>
<title>Welcome </title>
</head>

 <body>
 <h1>Welcome <?php echo $login_session; ?></h1>
 <h2><a href = "logout.php">Sign Out</a></h2>
 </body>

 </html>
 <?php
 session_start();

 if(session_destroy()) {
 header("Location: login.php");
 }
 ?>
Comments