how to redirect through user-home page - php

I am trying to redirect the index page to user-home.php page. but when I am trying to login its showing wrong user. How to redirect to the user-home page by this condition. expecting help. Thanks in advance. The following image is the database property of the signup page.
database of signup
HERE IS MY CODE FOR INDEX PAGE
<form method="post" action="" autocomplete="on">
<h3>Log in</h3>
<hr>
<p>
<label for="username" class="uname" data-icon="u" > Your username </label>
<input id="username" name="username" required="required" type="text"/>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<input id="password" name="password" required="required" type="password"/>
</p>
<p class="login button">
<input type="submit" name="login" value="Login" />
</p>
</form>
<h2>dont have an account signup here</h2>
<li>signup</li>
</table>
<?php
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$q=$db->prepare("SELECT* FROM signup WHERE username='$username' && password='$password'" );
$q->execute();
$row=$q->fetch(PDO::FETCH_OBJ);
$count = $q->rowcount();
if ($count >= 1){
session_start();
$_SESSION['id'] = $row['user_id'];
header('location:user-home.php');
}else{
echo "<script>alert ('Wrong User')</script>";
}
}
?>

Related

PHP Redirect to another page after login

Even though the if condition ($num>0) is true, I am not being redirected to the welcome.php, any help from fellow developers?
if(isset($_POST['login'])){
$password=$_POST['password'];
$email=$_POST['email'];
$ret= mysqli_query($con,"SELECT * FROM users WHERE email='$email' and password='$password'");
$num=mysqli_fetch_array($ret);
if($num>0)
{
$extra="welcome.php";
$_SESSION['login']=$_POST['email'];
$_SESSION['id']=$num['id'];
$_SESSION['full_name']=$num['full_name'];
$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("Location:http://$host$uri/$extra");
exit();
}
After login button is clicked, with the correct attributes, Im stuck on the same page, just blank white page.
<form name="login" action="" method="post">
<div class="uk-margin">
<div class="uk-inline">
<span class="uk-form-icon" uk-icon="icon: mail"></span>
<input class="uk-input" name="email" type="email" placeHolder="Enter your e-mail">
</div>
</div>
<div class="uk-margin">
<div class="uk-inline">
<span class="uk-form-icon" uk-icon="icon: lock"></span>
<input class="uk-input" name="password" type="password" placeHolder="Enter your password">
</div>
</div>
<input type="submit" name="login" value="LOG IN" >
</form>
replace header("location:http://$host$uri/$extra");
with header("Location: http://$host$uri/$extra");

Allow user to login with a username & password & 4-digit pin code

I want user login with username password and 4 digit pin code and code is in my db. Here is my code sample. If any one here can help me I would very much appreciate it:
<?php
if (isset($_POST['login'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
$pincode = $_POST['pincode'];
if (empty($user) OR empty($pincode)) {
echo "<script>alert('Please Fill All Required Field')</script>";
} else {
$select_user = "select * from users WHERE Username = '$user' AND Password ='$pass' pin-code ='?' ";
$run_user_sql = mysqli_query($conn, $select_user);
$check_customer = mysqli_num_rows($run_user_sql);
if ($check_customer ==false) {
echo "<script>alert('Username/Password Wrong')</script>";
exit();
}
if ($check_customer == true) {
$_SESSION['user'] = $user;
echo "<script>alert('You Are Logged In')</script>";
echo "<script>window.open('index.php?dashboard','_self')</script>";
}
}
}
?>
<form action="" method="post">
<div class="form-group">
<div class="form-label-group">
<input type="text" class="form-control" name="user" placeholder="Username" >
<label">Username</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" class="form-control" name="pass" placeholder="Password" >
<label >Password</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="text" class="form-control" name="pincode" placeholder="4-Digit Pin Code" >
<label >4-Digit Pin Code</label>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me">
Remember Password
</label>
</div>
</div>
<input type="submit" class="btn btn-primary btn-block" name="login" value="Login">
</form>
Your code seems to be okay, but with a lot of security risks. However, your SQL command is wrong. Change it to the following:
$select_user = "select * from users WHERE Username = '$user' AND Password ='$pass' AND `pin-code` ='{$pincode}'";

how to view the cookies value in my login page?

I have worked on "keep me logged in" in my login page in smarty. I want to view the email and password on my login page after login and i have some problem because cookies value are always store in my browser but not viewing in my login page.
my php code are:
if (isset($_REQUEST['login'])) {
// $email=$_REQUEST['email'];
// $pass=$_REQUEST['pass'];
if (isset($_REQUEST['keeplogin'])) {
setcookie("Email", $_REQUEST['email'], time() + 3600);
setcookie("Pass", $_REQUEST['pass'], time() + 3600);
$email = $_COOKIE['Email'];
$pass = $_COOKIE['Pass'];
}
$smarty->assign('email', $email);
$smarty->assign('pass', $pass);
}
and my login page
<form role="form" action="index.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="pwd">Email ID:</label>
<input class="form-control" type="email" placeholder="example#mail.com" name="email" value="{$smarty.cookies.$email}" required="required" />
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input class="form-control" type="password" name="pass" value="{$pass}" required="required" />
<h5 align="right">Forgot Password<span class="badge"></span></h5>
</div>
<div class="login button">
<input type="submit" class="btn btn-primary" name="login" value="Login" style="width:100%; height:40px;" />
</div>
<p class="keeplogin">
<input type="checkbox" name="keeplogin" id="loginkeeping" value="keep_login" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<input type="hidden" name="act" value="{$act}" />
<input type="hidden" name="u_id" value="{$u_id}" />
</form>
how to view a email and password with smarty in login page?
You can get cookie value in php like this
echo "<b>Email:</b>".$_COOKIE['Email'];
echo "<b>Pass:</b>".$_COOKIE['Pass'];

php login script not redirecting to next page

My login.php is as given below, It is not able to take me to welcome.php. Neither it is giving login error. It simply give 500 error page.
<?php
include("db.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$username=mysqli_real_escape_string($db,$_POST['username']);
$password=mysqli_real_escape_string($db,$_POST['password']);
$password=md5($password); // Encrypted Password
$sql="SELECT id FROM admin WHERE username='$username' and passcode='$password'";
$result=mysqli_query($db,$sql);
$count=mysqli_num_rows($db,$result);
// If result matched $username and $password, table row must be 1 row
if($count==1)
{
header("location: welcome.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>
<div class="wrapper">
<form class="form-signin" action="login.php" method="post">
<h2 class="form-signin-heading">Please login</h2>
<input type="text" class="form-control" name="username" placeholder="User Name" required="" autofocus="" />
</br>
<input type="password" class="form-control" name="password" placeholder="Password" required=""/>
<label class="checkbox">
<input type="checkbox" value="remember-me" id="rememberMe" name="rememberMe"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</form>
</div>
It gives 500 error after I submit usename and password and not able to take me to welcome.php.
If you are using header(Location: something.php), you should load it as first output in php. Or else it will not work.
session_start() will change the HTTP Header.
Instead try below one,
echo "<script>window.location.href='welcome.php';</script>";

override include php if logged in

I'm working on a login system. I want when I login at index.php, the include from that page change to another. So in my case the fully header change and the login button disappear.
So I want to change <?php include('header.php'); ?>
to
<?php include('header2.php'); ?>
if logged in
The login:
<div id="loginContainer">
<span>Login</span><em></em>
<div style="clear:both"></div>
<div id="loginBox">
<form id="loginForm" action="login.php" autocomplete="on" method="post">
<fieldset id="body">
<fieldset>
<label for="email">Email Address</label>
<input id="username" name="username" required="required" type="text" placeholder="myusername"/>
</fieldset>
<fieldset>
<label for="password">Password</label>
<input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" />
</fieldset>
<input type="submit" name="login" value="Login" />
<label for="checkbox"><input type="checkbox" id="checkbox" />Remember me</label>
</fieldset>
<span>Forgot your password?</span>
</form>
</div>
</div></
Part of login.php
if($res = $mysqli->query($q))
{
if($res->num_rows > 0)
{
$_SESSION['userName'] = $userName;
//header("Location:welcome.php");
include('header4.php');
exit;
}
else
{
echo'<script>alert("INVALID USERNAME OR PASSWORD");</script>';
header("Location:index.php");
exit;
}
}
Simply check if a session is set, and then include the right file. Something like this.
if (isset($_SESSION['userName']) && !empty($_SESSION['userName'])) {
// If the user is logged in, include this file
include "header2.php";
} else {
// Else, we're not logged in, include this file
include "header.php";
}

Categories