Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP My Multi User Login System Won't Work

Brandon3000

New Coder
I've been trying to make a multi user login system but when I try to log on it stays at the login page (index.php) page. What can I do?

index.php

PHP:
<form method="post" class="form-horizontal">

    <div class="form-group">
        <label class="col-sm-3 control-label">Email</label>
        <div class="col-sm-6">
            <input type="text" name="txt_email" class="form-control" placeholder="enter email" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Password</label>
        <div class="col-sm-6">
            <input type="password" name="txt_password" class="form-control" placeholder="enter password" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Select Type</label>
        <div class=col-sm-3>
            <select class="form-control" name="txt_role">
                <option value="" selected="selected"> - Which one are you </option>
                <option value="admin"> Admin </option>
                <option value="parent"> Parent </option>
                <option value="swimmer"> Swimmer </option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9 m-t-15">
            <input type="submit" name="btn_login" class="btn btn-success" value="Login">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset col-sm-9 m-t-15">
            You don't have an account at Water Tower 2000 <a href="register.php">
                <p class="text-info">Register Water Tower 2000 Account </p>
            </a>
        </div>
    </div>
</form>

index.php PHP Code

PHP:
<?php
require_once 'connection.php';
session_start ();

if(isset ($_SESSION["admin_login"]))

{
    header ("location:admin/admin_home.php");
}

if(isset ($_SESSION["parent_login"]))

{
    header ("location:parent/parent_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:swimmer/swimmer_home.php");
}


if (isset ($_GET['btn_login'])) {

    $email = $_GET ["txt_email"];

    $password = $_GET ["txt_password"];

    $email = $_GET ["txt_role"];

if(empty($email)){

    $errorMsg[]="please enter your water tower email";

}

else if(empty($password)){

    $errorMsg[]="please enter yout water tower email";
}

else if(empty($role)){

    $errorMsg[]="please enter yout water tower email";

}


else if($email AND $password AND $role){

try{

$select_stmt=$db->prepare("SELECT email,password,role FROM masterlogin WHERE email=:uemail

AND password=:upassword AND role=:urole");

$select_stmt->bindParam(":uemail",$email);

$select_stmt->bindParam(":upassword",$password);

$select_stmt->bindParam(":urole",$role);

$select_stmt->excute();

while ($row=$select_stmt->fetch(PDO::FETCH_ASSOC)){

    $dbemail =$row["email"];

    $dbpassword =$row["password"];

    $dbrole =$row["role"];

}
    
if($email!=null AND $password!=null AND $role!=null){

if($select_stmt->rowCount()>0){

if ($email!==$dbemail AND $password==$dbpassword AND $role==$dbrole){

switch($dbrole) {

case "admin":

$_SESSION ["admin_login"]=$email;
$loginMsg="Admin...Your in Water Tower...";
header("refresh:3;admin/admin_home.php");
break;

case "parent":
$_SESSION["parent_login"]=$email;
$loginMsg="Parent...Welcome To Water Tower...";
header("refresh:3;parent/parent_home.php");
break;

case "swimmer":
$_SESSION ["swimmer_login"]=$email;
$loginMsg="Fellow swimmer...Your in Water Tower...";
header("refresh:3;swimmer/swimmer_home.php");
break;

default:
$errorMsg[]="Sorry but either the email/password/role is wrong";
}

}



else {

$errorMsg="Sorry but either the email/password/role is wrong";
}

}

else {

$errorMsg="Sorry but either the email/password/role is wrong";

}

}

else{

$errorMsg="Sorry but either the email/password/role is wrong";

}

}



catch (PDOException $e){

$e->getMassage();

}

}

else {

$errorMsg="Sorry but either the email/password/role is wrong";

}

}

?>

register.php

PHP:
<form method ="post" class="form-horizontal">

<div class ="form-group">
<label class = "col-sm-3 control-label">Username</label> 
<div class = "col-sm-6"> 
<input type = "text" name="txt_username" class="form-control" placeholder = "enter username" />
</div>
</div>

<div class ="form-group">
<label class = "col-sm-3 control-label">Password</label> 
<div class = "col-sm-6"> 
<input type = "password" name="txt_password" class="form-control" placeholder = "enter password" />
</div>
</div>

<div class ="form-group">
<label class = "col-sm-3 control-label">Username</label> 
<div class = "col-sm-6">
<select class = "" name="txt_role">
<option value = "" selected = "selected"> - select role - </option>
<option value = "swimmer">Swimmer</option>         
<option value = "parent">Parent</option>
</select>   
</div>
</div>

<div class = "form-group">
<div class = "col-sm-offset-3 col-sm-9- m-t-15">   
<input type = "submit" name="btn_register" class="btn btn-primary" value = "Register">
</div>
</div>

<div class = "form-group">
<div class ="col-sm-offset-3 col-sm-9 m-t-15">
You have an account at Water Tower? <a href = "index.php"><p class = "text-info">Login Account </p></a>       
</div>
</div>

</form>

register.php PHP Code

PHP:
<?php

require_once 'connection.php';

if(isset ($_GET["btn_register"]))

{

if (isset ($_GET['btn_login'])) {
    $username = $_GET ["txt_username"];
    $email = $_GET ["txt_email"];
    $password = $_GET ["txt_password"];
    $role = $_GET ["txt_role"]


if(empty($username)){
    $errorMsg[]="please enter your usernmae";
}

else if(empty($email)){
    $errorMsg[]="please enter your email";
}

else if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
    $errorMsg[]="please enter a valid email address ";
}

else if(empty($password)){
    $errorMsg[]="please enter password";
}   

else if (strlen ($password) < 6) {
    $errorMsg[]="It needs to be more than 6 characters";
}

else if (empty($role)) < 6) {
    $errorMsg[]="Please select a role";
}


try{
$select_stmt=$db->prepare("SELECT username,email FROM masterlogin WHERE username=:uname OR
email=:uemail");

$select_stmt->bindParam(":uname",$username);
$select_stmt->bindParam(":uemail",$email);
$select_stmt->excute();
$row=$select_stmt->fetch(PDO::FETCH_ASSOC);


if ($row["username"]==$username){
    $errorMsg[]="Sorry but username already exsit";
}

else if ($row["email"]==$email){
    $errorMsg[]="Sorry but email already exsit";
}

else if($isset($errorMsg))

{

$insert_stmt=$db->prepare ("INSERT INTO masterlogin (username,email,password,root) VALUES(:uname,:uemail,:upassword,:urole)");
$insert_stmt->bindParam(":uname",$username);
$insert_stmt->bindParam("uemail",$uemail);
$insert_stmt->bindParam("upassword",$upassword);
$insert_stmt->bindParam("urole",$role);

if($insert_stmt->excute())

{

$registerMsg = "Register Succesfully...Wait Login Page";   
header("refresh:4;index.php");

       }

   }

 }

 catch (PDOException $e) {

    echo $e->getMassage();

 }

}

}

?>

connection.php

PHP:
<?php

$db_host="localhost";
$db_user="root";
$db_password= "";
$db_name="php_water tower 2000";

try {

    $db=new PDO ("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_password);
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

}

catch (PDOEXCEPTION $e) {
    $e->getMessage();
}

?>

admin_home.php

PHP:
<style type = "text/css">

body {
    padding:0;
    margin:0;
    background:
}
    
h1 {

    font-size:5em;
    margin:0;
    padding:0;
    text-align:center;
    font-family: 'arial';
    position:absolute;
    top:50%;
    left:50%;
    transform: translateX(-50%);

}

</style>
</body>

<body>
<h1> This is the Admin Page </h1>
</body>


<?php

session_start();

if(isset ($_SESSION["admin_login"]))
{
    header ("location:.../index.php");
}

if(isset ($_SESSION["parent_login"]))
{
    header ("location:parent/parent_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:swimmer/swimmer_home.php");
}

if(isset ($_SESSION["admin_login"]))
{
 ?>
Welcome,
<?php
echo $_SESSION ['admin_login'];

}
?>

<a href = "../logout.php">Logout</a>

parent_home.php

PHP:
<style type = "text/css">

body {
    padding:0;
    margin:0;
    background:
}
    
h1 {

    font-size:5em;
    margin:0;
    padding:0;
    text-align:center;
    font-family: 'arial';
    position:absolute;
    top:50%;
    left:50%;
    transform: translateX(-50%);

}

</style>
</body>

<body>
<h1> This is the Parent Page </h1>
</body>

<?php

session_start();

if(isset ($_SESSION["parent_login"]))
{
    header ("location:.../index.php");
}

if(isset ($_SESSION["admin_login"]))
{
    header ("location:.../admin/admin_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:.../swimmer/swimmer_home.php");
}

if(isset ($_SESSION["parent_login"]))
{
 ?>
Welcome,
<?php
echo $_SESSION ['parent_login'];

}
?>

<a href = "../logout.php">Logout</a>

swimmer_home.php

PHP:
<style type = "text/css">

body {
    padding:0;
    margin:0;
    background:
}
    
h1 {

    font-size:5em;
    margin:0;
    padding:0;
    text-align:center;
    font-family: 'arial';
    position:absolute;
    top:50%;
    left:50%;
    transform: translateX(-50%);

}

</style>
</body>

<body>
<h1> This is the Swimmer Page </h1>
</body>

<?php

session_start();

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:.../index.php");
}

if(isset ($_SESSION["admin_login"]))
{
    header ("location:.../admin/admin_home.php");
}

if(isset ($_SESSION["parent_login"]))
{
    header ("location:.../parent/parent_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
 ?>
Welcome,
<?php
echo $_SESSION ['swimmer_login'];

}
?>

<a href = "../logout.php">Logout</a>

logout.php

PHP:
<style type = "text/css">

body {
    padding:0;
    margin:0;
    background:
}
    
h1 {

    font-size:5em;
    margin:0;
    padding:0;
    text-align:center;
    font-family: 'arial';
    position:absolute;
    top:50%;
    left:50%;
    transform: translateX(-50%);

}

</style>
</body>

<body>
<h1> This is the Swimmer Page </h1>
</body>

<?php

session_start();

if(isset ($_SESSION["swimmer_login"]))
{
    header ("location:.../index.php");
}

if(isset ($_SESSION["admin_login"]))
{
    header ("location:.../admin/admin_home.php");
}

if(isset ($_SESSION["parent_login"]))
{
    header ("location:.../parent/parent_home.php");
}

if(isset ($_SESSION["swimmer_login"]))
{
 ?>
Welcome,
<?php
echo $_SESSION ['swimmer_login'];

}
?>

<a href = "../logout.php">Logout</a>
 
Just looking at your main HTML page I see that you don't have a destination for your form. You need an "action" attribute to give it a place to be submitted.
You also have method as POST but in the PHP file you are looking for GET.
Code:
if (isset ($_GET['btn_login'])) {

    $email = $_GET ["txt_email"];

$password = $_GET ["txt_password"];

$email = $_GET ["txt_role"];
Not sure if you close the if statement.
 

New Threads

Buy us a coffee!

Back
Top Bottom