php valid session redirecting to indexpage - php

I am create a login page which will redirect to home.php page after login valid.
USING SESSION FOR THIS .
But problem is after login its redirect to index.php page.
But it should redirect to home.php
header.php
<?php
session_start();
$valid = $_SESSION['valid'];
if(!$valid || $valid ==""){
header("Location:index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Management System</title>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapperMain">
index.php
<?php
session_start();
if(isset($_SESSION['valid'])){
header("Location:home.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Doctor's BD</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<!--Header Area Start-->
<div class="header-custom navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle navbar-tg" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="header-logo">
<img src="img/logo.png" alt="" class="img-responsive logo">
</div>
</div>
<div class="navbar-collapse collapse" id="navbar-main">
<form class="login-form-style navbar-form navbar-right" role="search" action="login.php" id="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<div class="form-group">
<input type="email" class="form-control" name="d_email" placeholder="Email address">
</div>
<div class="form-group">
<input type="password" class="form-control" name="d_pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Sign In</button>
<br>
</form>
</div>
</div>
</div>
<!--Header Area End-->
<?php include 'content.php';?>
<?php include 'footer.php';?>
home.php
<?php include 'header.php';
?>
<?php
if($_SESSION['valid']=='admin#gmail.com')
{
include 'ahome.php';
}
else
{
include 'dhome.php';
}
?>
<?php include 'footer.php';?>
login.php
<!--Login Verification Area Start-->
<?php
include 'config.php';
$d_email=$_POST['d_email'];
$d_pass=$_POST['d_pass'];
$m_d_pass=md5($d_pass);
$result= mysql_query("select * from doctor_reg where d_email='$d_email' and d_pass='$m_d_pass'",$connection) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if(is_array($row) && !empty($row))
{
$validuser = $row['d_email'];
$_SESSION['valid'] = $validuser;
}
else{
header('Refresh: 5; url=index.php');
echo "<strong style='color: #3c763d;text-align:center;'><h3>Access denied!</h3>";
echo "<h4>The user id or password you entered is incorrect</h4></strong>";
}
?>
<?php
if(isset($_SESSION['valid']))
{
header("Location: home.php");
}
?>
<!--Login Verification Area End-->
<!---->
<!---->

First thing, do all session check in a single file header.php and include this file in all files.
In header.php, modify following code:
<?php
session_start();
$valid = $_SESSION['valid'];
if(!$valid || $valid ==""){
header("Location:index.php");
}
else {
header("Location: home.php");
}
?>

Related

how to prevent from going back to login page after logging in

I have developed a website in php. The index.php is a login form. After logging in dashboard.php is coming. But when I press the back button in the browser it is redirecting to the login page. How to prevent it. If there is any solution please tell. Thanks in advance. The codes are given below:
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body style="background-image: url('https://www.pixelstalk.net/wp-content/uploads/2016/05/HD-Black-Picture.jpg');">
<section id="login">
<div class="container">
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8 col1">
<div class="row">
<div class="col-sm-7 co2">
<h1 class="h1">Orbit Shifters Employee Site</h1>
<h2 class="h2">Login Here <i class="fas fa-long-arrow-alt-right"></i></h2>
</div>
<div class="col-sm-5 co1">
<form method="post" action="func.php">
<div>
<input type="text" name="username" class="i1" placeholder="Enter Your Username">
</div>
<div>
<input type="password" name="password" class="i1" placeholder="Enter Your Password">
</div>
<div>
<input type="submit" name="submit" class="btn btn1">
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-2"></div>
</div>
</div>
</section>
</body>
</html>
func.php
<?php
session_start();
$con=mysqli_connect("localhost","root","","login");
$connect = new PDO('mysql:host=localhost;dbname=login', 'root', '');
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from signup where username='$username' and password='$password';";
$result=mysqli_query($con,$query);
$row=mysqli_fetch_assoc($result);
if(mysqli_num_rows($result)==1)
{
$_SESSION["username"] = $username;
$_SESSION['status']="Active";
header("Location:dashboard.php?name=".$row['name']);
exit;
}
else{
echo "<script>alert('Enter Correct Details!!')</script>";
echo "<script>window.open('index.php', '_self')</script>";
}
}
?>
dashboard.php
<?php
session_start();
if($_SESSION['status']!="Active")
{
header("location:index.php");
}
else{
$name=$_GET['name'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body style="background:url(https://i.pinimg.com/originals/e5/f3/af/e5f3af2b9186af6e86187c84f4ad930e.jpg);">
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="dashboard.php?name=<?php echo $name; ?>">Dashboard</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li class="li1"><span class="glyphicon glyphicon-log-in"></span> Logout</li>
</ul>
</div>
</div>
</nav>
<section id="dashboard">
<div class="container">
<div class="row ro1">
<p class="p1"> Hello <?php echo $name; ?>, Welcome to Orbit Shifters EMployee Site.</p>
</div>
<div class="ro1">
<div class="col-sm-4 col2">
<button class="btn btn2">Project 1 <br>Report <br>Submission</button>
</div>
<div class="col-sm-4 col2">
<button class="btn btn2">Project 2 <br>Monthly Report <br>Submission</button>
</div>
<div class="col-sm-4 col2">
<button class="btn btn2">Project 3 <br>Feedback <br>Submission</button>
</div>
</div>
</div>
</section>
</body>
</html>
<?php
}
?>
logout.php
<?php
session_start();
session_destroy();
$_SESSION = array();
unset($_SESSION['username']);
unset($_SESSION['status']);
header("Location:index.php");
?>
I am not sure if I understand the problem correctly, but what if you add a check at the beginning of the index.php file that would redirect you to the dashboard if you are logged in ? Something like this
// index.php
<?php
session_start();
if (isset($_SESSION['status']) && $_SESSION['status'] === "Active") {
header("location: dashboard.php");
}
?>
<!DOCTYPE html>
<html>
<head>
...
this way, if you click back in the browser, you will still go to index.php, but then you will be redirected to the dashboard again if you are already logged in

PHP: Clicking back button still redirects to the previous page

Really new with PHP. I have a login page that directs to an index page. On the index page there is a logout button. So when I click the logout, It will redirect to the login page and will unset the $_SESSION when i click the back button. But it does not destroy the session. It still redirects me to the index page. But with unset values already. Seems like session_destroy(); is not working. If anyone can help me, it would really be great. Thanks!
This is my login.php
<?php
require('session.php');
?>
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="css/style-login.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="header">
<img src = "img/usc_logo.png">
</div>
<div class="box">
<form id="loginform" class="form-horizontal" role="form" action = "" method = "POST">
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" name="username" value="" placeholder="ID Number" required/>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<div style="margin-top:10px" class="form-group">
<!-- Button -->
<div class="col-sm-12 controls">
<input class="submit-1" type = "submit" value = " Log in " name= "submit"/><br/><br/>
Not a member yet?
Register<br/><br/>
<center>
<span style='color:#e60000; font-size:15px;'><b><?php echo $error; ?></b></span>
</center>
</div>
</div>
</form>
</div>
<!-- JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
And this is my index-admin.php
<?php
require('session.php');
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.5">
<title>Admin DCIS</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.3/examples/album/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="album.css" rel="stylesheet">
</head>
<body>
<header>
<div class="collapse bg-dark" id="navbarHeader">
<div class="container">
</div>
</div>
<div class="navbar navbar-dark bg-dark shadow-sm">
<div class="container d-flex justify-content-between">
<a href="#" class="navbar-brand d-flex align-items-center">
<strong>Hello, <?php echo $_SESSION['user_role'] ?></strong>
</a>
Logout
</div>
</div>
</header>
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1 class="jumbotron-heading">Name of Requestee</h1>
<button href="#" class="btn btn-primary my-2">Accept</button>
<button href="#" class="btn btn-secondary my-2">Reject</button>
</p>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
This is the logout.php
<?php
session_start();
session_unset();
session_destroy();
header('Location: login.php');
?>
In case needed, this is my session.php
<?php
require("dbconnector.php");
$error="";
session_start();
if(isset($_POST['submit'])){
require ('dbconnector.php');
$query = "SELECT * FROM user WHERE username = '{$_POST['username']}' AND password = '{$_POST['password']}'";
$con = mysqli_query($conn, $query);
if ($con->num_rows == 1){
$row = mysqli_fetch_assoc($con);
$_SESSION['user_role'] = $row['user_role'];
mysqli_close($conn);
if ($_SESSION['user_role'] == 'Admin'){
header('Location: index-admin.php');
}
elseif($_SESSION['user_role'] == 'Student') {
header('Location: index.php');
}
}
else{
$error = "ID Number or Password is Invalid";
header("refresh: 5");
}
}
?>
You need to properly destroy the session and the session's cookie on client side. I would recommend to not write this on the login page but rather on a specific page like logout.php that would redirect to login.html.
<?php
session_start(); // Start by calling the session.
$_SESSION = array(); // Reset the session's variables.
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]); //Invalids session's cookie.
session_destroy(); //Finally destroy it.
//If you want to redirect
header("Location: <URL>");
?>

Display signature image in email

I was looking for a signature pad and came across a plugin by Thomas Bradley.
I'm trying ti display the signature as an image in an email. All I get is an image code
Client signature:
[{"lx":69,"ly":44,"mx":69,"my":43},{"lx":68,"ly":43,"mx":69,"my":44},{"lx":68,"ly":44,"mx":68,"my":43},{"lx":69,"ly":48,"mx":68,"my":44},{"lx":72,"ly":55,"mx":69,"my":48},{"lx":77,"ly":67,"mx":72,"my":55},{"lx":85,"ly":82,"mx":77,"my":67},{"lx":90,"ly":96,"mx":85,"my":82},{"lx":93,"ly":107,"mx":90,"my":96},{"lx":95,"ly":114,"mx":93,"my":107},{"lx":97,"ly":117,"mx":95,"my":114},{"lx":98,"ly":117,"mx":97,"my":117},{"lx":102,"ly":113,"mx":98,"my":117},{"lx":110,"ly":102,"mx":102,"my":113},{"lx":120,"ly":86,"mx":110,"my":102},{"lx":131,"ly":68,"mx":120,"my":86},{"lx":139,"ly":53,"mx":131,"my":68},{"lx":143,"ly":48,"mx":139,"my":53},{"lx":145,"ly":47,"mx":143,"my":48},{"lx":147,"ly":49,"mx":145,"my":47},{"lx":154,"ly":55,"mx":147,"my":49},{"lx":159,"ly":62,"mx":154,"my":55},{"lx":161,"ly":68,"mx":159,"my":62},{"lx":162,"ly":73,"mx":161,"my":68},{"lx":162,"ly":75,"mx":162,"my":73},{"lx":162,"ly":74,"mx":162,"my":75},{"lx":166,"ly":66,"mx":162,"my":74},{"lx":173,"ly"
:53,"mx":166,"my":66},{"lx":180,"ly":37,"mx":173,"my":53},{"lx":182,"ly":27,"mx":180,"my":37},{"lx":182,"ly":23,"mx":182,"my":27},{"lx":178,"ly":31,"mx":182,"my":23},{"lx":169,"ly":45,"mx":178,"my":31},{"lx":163,"ly":59,"mx":169,"my":45},{"lx":161,"ly":66,"mx":163,"my":59},{"lx":163,"ly":68,"mx":161,"my":66},{"lx":170,"ly":64,"mx":163,"my":68},{"lx":183,"ly":55,"mx":170,"my":64},{"lx":205,"ly":43,"mx":183,"my":55},{"lx":230,"ly":32,"mx":205,"my":43},{"lx":267,"ly":22,"mx":230,"my":32},{"lx":300,"ly":12,"mx":267,"my":22},{"lx":307,"ly":9,"mx":300,"my":12},{"lx":308,"ly":8,"mx":307,"my":9}]
PHP Code:
<?php
require_once("session.php");
require_once("class.user.php");
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$stmt = $auth_user->runQuery("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if ($_POST['submit']) {
if (!$_POST['output']) {
$error = "<br>- Please enter your signature!";
}
if ($error) {
$result = "<div class='alert alert-danger' role='alert'>Whoops, there is an error. Please correct the following: $error</div>";
} else {
mail("albetws#gmail.com", "Request form", "From: ".$_POST['username']."
Message: ".$_POST['message']."
Client signature: ".$_POST['output']);
{
$result = "<div class='alert alert-success text-center' role='alert'>
<p>Thank you for your request.</p>
<p>No request conformation within 1 hr call the receiver</p>
<p>Please <a href='logout.php?logout=true'>Logout</a>.</p>
</div>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Title</title>
<!-- Bootstrap -->
<link href="signature/assets/jquery.signaturepad.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<!--[if lt IE 9]><script src="../assets/flashcanvas.js"></script><![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
</head>
<body>
<section>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-4 col-sm-offset-4">
<h2 class="text-center">Client Approval</h2>
<p class="text-center">Return to Main Menu &sol; Logout</p>
<?php echo $result;?>
<form action="" method="post" role="form" class="sigPad">
<p class="drawItDesc">Insert signature in the box below.</p>
<div class="sig sigWrapper">
<canvas class="pad" height="150"></canvas>
<input type="hidden" name="output" class="output">
</div>
<div class="form-group input-group btn-margin">
<span class="input-group-addon">
<span class="glyphicon glyphicon-remove"></span>
</span>
<input type="button" name="clear" class="form-control btn btn-primary clearButton" value="Clear signature">
</div>
<div class="form-group input-group btn-margin">
<span class="input-group-addon">
<span class="glyphicon glyphicon-send"></span>
</span>
<input type="submit" name="submit" class="form-control btn btn-primary" value="Submit signature">
</div>
</form>
</div>
</div>
</div>
</section>
<script src="signature/jquery.signaturepad.js"></script>
<script>
$(document).ready(function() {
var options = {
defaultAction: 'drawIt',
drawOnly: true,
lineTop: 135,
lineMargin: 20,
penColour: '#000'
}
$('.sigPad').signaturePad(options);
});
</script>
<script src="signature/assets/json2.min.js"></script>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Thank you
I used a litte time befor szimek pad signature under MIT License from github.com web site,
otherwise i don't see in your code in wich format you save you image.

Display username with cookies

I have created a connection between php and mysql ,and i added a username and password on mysql table so i can login and redirect to a "members" page. It works at this point but what i want to do is to display "username" in some places in my "members" page with cookies.
On first.php , on navbar, blockquote where it says "User" thats where i want to replace it with the username that logins but with cookies and i haven't understood how to do it
My files:
config.php
<?php
$mysql_hostname="localhost";
$mysql_user="root";
$mysql_password="root";
$mysql_database="Electricians";
$bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or die("Bad Connection");
mysql_select_db($mysql_database,$bd)or die("Bad Connection");
?>
index.php
<?php
header("location:login.php");
?>
login.php
<?php
session_start();
include("includes/config.php");
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$myusername=addslashes($_POST ['username']);
$mypassword=md5(addslashes($_POST[ 'password']));
$sql="SELECT userid FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
$_SESSION['login_admin']=$myusername;
header("location:http://localhost:8888/offlineproject/first.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/theme.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/csc" href="css/bootstrap-glyphicons.css">
<link rel="stylesheet" type="text/csc" href="css/login.css">
<title>Online Drawing for Electricians</title>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-header">
<a class="navbar-brand" href="#"><img id="logoattop" src="images/nklogo.png" alt="Logo" >Online Drawing for Electricians </a>
</div>
</div>
</nav><
<div class="jumbotron">
<div class="container text-center">
<h1>Online Drawing for Electricians</h1>
<div class="container">
<form class="form-signin" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input name="username" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
Skip
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-2.1.3.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
first.php
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/theme.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link href="css/bootstrap-glyphicons.css"rel="stylesheet">
<title>Online Drawing for Electricians</title>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!navbar header-->
<div class="navbar-header">
<a class="navbar-brand" href="#"><img id="logoattop" src="images/nklogo.png" alt="Logo" >Online Drawing for Electricians </a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li></span> <User></li>
<li><span class="glyphicon glyphicon-log-out"></span> Logout</li>
</ul>
</div>
</div><!End-Container-->
</nav><!End-Nav-Bar-->
<div class="jumbotron">
<div class="container text-center">
<h1>Online Drawing for Electricians</h1>
<div class="btn-group">
Search
Upload
</div>
</div>
</div> <!End Jumbotron-->
<!Blockquotes-->
<div class="container">
<section>
<div class= "page-header" id="feedback">
<h2>Recent activity of <small><User></small></h2>
</div>
<div class="row">
<div class="col-lg-12">
<blockquote>
<p>"User" Today job is to fix the cables of the central bank.</p>
<p>Cables need to be fixed as soon as possible </p>
<footer>NK Electrical LTD</footer>
</blockquote>
</div>
<div class="col-lg-12">
<blockquote>
<p>Something Something
SomethingSomething
SomethingSomethingSomething</p>
<footer>Aris Con</footer>
</blockquote>
</div>
</div>
</section>
Skip
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-2.1.3.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
logout.php
<?php
session_start();
if(session_destroy())
{
header("location: index.php");
}
?>
Just because a user/pass is set, you can't presume they are an admin, unless there is some further checking done. Either via a flag in the database or by matching a hardcoded value somewhere.
So you need to change this up to something like this:
$sql="SELECT userid FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
$row = mysql_fetch_assoc($result);
if($row['is_admin'] == '1'){
$_SESSION['login_admin']=$myusername;
header("location:http://localhost:8888/offlineproject/first.php");
exit;
}
else{
// do something else here
}
}
}
Try this for set cookie
$first_name = 'Rilakkuma';
setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day
And for call cookie
$_COOKIE['first_name'];
See more here --> http://davidwalsh.name/php-cookies
$first_name = "user";
then setcookie(); use this one for setting cookie
$COOKIE['first_name']

How to hide a div on php and html

im new on this section (html php...)
I have a code, is a login form, and, i want, to hide the red box of (Incorrect user or password) from the login form but i dont know how can i do this.
An screenshot: http://prntscr.com/5daqsh
the code is:
<head>
<meta charset="utf-8" />
<title>My Website</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="css/animate.css" type="text/css" />
<link rel="stylesheet" href="css/font-awesome.min.css" type="text/css" />
<link rel="stylesheet" href="css/font.css" type="text/css" />
<link rel="stylesheet" href="js/fuelux/fuelux.css" type="text/css" />
<link rel="stylesheet" href="css/app.css" type="text/css" />
<body background="images/login-bg.jpg">
<!--[if lt IE 9]>
<script src="js/ie/html5shiv.js"></script>
<script src="js/ie/respond.min.js"></script>
<script src="js/ie/excanvas.js"></script>
<![endif]-->
</head>
<body>
<section id="content" class="m-t-lg wrapper-md animated fadeInUp">
<div class="container aside-xxl">
<a class="navbar-brand block" href="index.html">DameFans</a>
<section class="panel panel-default bg-white m-t-lg">
<header class="panel-heading text-center">
<strong>Iniciar Sesión</strong>
</header>
<form action="" method="post" class="panel-body wrapper-lg">
<div class="form-group">
<div class="alert alert-danger">
<?php
session_start();
include_once "conexion.php";
function verificar_login($user,$password,&$result)
{
$sql = "SELECT * FROM users WHERE login='$user' and pass='$password'";
$rec = mysql_query($sql);
$count = 0;
while($row = mysql_fetch_object($rec))
{
$count++;
$result = $row;
}
if($count == 1)
{
return 1;
}
else
{
return 0;
}
}
if(!isset($_SESSION['userid']))
{
if(isset($_POST['login']))
{
if(verificar_login($_POST['user'],$_POST['password'],$result) == 1)
{
$_SESSION['userid'] = $result->id;
header("location:index.html");
}
else
{
echo '<div class="error">Su usuario es incorrecto, intente nuevamente.</div>';
}
}
?>
</div>
<label class="control-label">Email</label>
<input name="user" type="text" class="form-control input-lg">
</div>
<div class="form-group">
<label class="control-label">Contraseña</label>
<input name="password" type="password" class="form-control input-lg">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Mantener mi sesión
</label>
</div>
<small>Recuperar contraseña</small>
<button type="submit" name="login" type="submit" value="login" class="btn btn-primary">Acceder</button>
<div class="line line-dashed"></div>
<i class="fa fa-facebook pull-left"></i>Acceder vía Facebook
<i class="fa fa-twitter pull-left"></i>Acceder vía Twitter
<div class="line line-dashed"></div>
<p class="text-muted text-center"><small>¿Aún no tienes cuenta?</small></p>
Crear cuenta ahora
</form>
</section>
</div>
</section>
<div class="alert alert-success">
<?php
} else {
echo '<i class="fa fa-ban-circle"></i><strong>Ha accedido correctamente</strong> <a href="index.html" class="alert-link">Serás redirigido al panel automáticamente en breve.';
echo 'Cerrar Sesión<br>';
echo '<meta http-equiv="refresh" content="3;url=index.html">';
}
?>
</div>
If anyone can help me to hide the block until a user fail login in or something please :)
First, you have to put your "session_start()" in the first line of your code file.
Second, Php is the server language. So, it will be excute before any others statics language.
In the footer, put something like this (i presume you have jQuery)
$('.alert').hide(); //hide on load page
Put your HTML normaly and in the bottom, make your login check.
If the check is in error, do this :
$('.alert').show();
And that's it.
If your question is "How to show HTML/JS in PHP?", do this :
<?php echo "$('.alert').show();"?>;
in a JS block and "document ready".

Categories