Part of PHP only runs if the form is submitted twice - php

In a very basic login page I set up an error status to show if the form was submitted with an empty pin or an invalid pin was entered but it only functions if I submit the form twice with the same error.
Where am I going wrong here?
Or else what would be a better way to achieve this?
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
?>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
</head>
<body>
<h1>Page Title</h1>
<div class="smallcontainer">
<form method="post" action="">
<div class="row">
<div class="col-20">
<label for="id">Driver Pin</label>
</div>
<div class="col-80">
<input type="text" name="id" >
</div>
</div>
<div class="row">
<div class="col-100 error">
<?= #$_SESSION['status'] ?>
</div>
<div class="col-100">
<input type="submit" value="Submit" name="submit">
</div>
</div>
</form>
</div>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$id = trim($_POST['id']);
if (empty($_POST['id'])){
$_SESSION['status'] = 'Enter a Driver Pin';
}
// if (!strlen($id)) {
// $_SESSION['status'] = 'Enter a Driver Pin';
// //die('Please enter Driver Pin');
// }
$success = false;
$handle = fopen("users.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
if ($data[0] == $id) {
$success = true;
$_SESSION['displayname'] = $data[1];
break;
}
}
fclose($handle);
if ($success) {
$_SESSION['allow'] = '';
header('Location: ./');
} else {
$_SESSION['status'] = 'Invalid Driver Pin - Try again';
}
}
?>

First you need to put the php before the form (as already said) and here is your code with a few fixes:
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (isset($_POST['submit'])) {
$id = trim($_POST['id']);
$success = false;
$handle = fopen("users.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
if ($data[0] == $id) {
$success = true;
$_SESSION['displayname'] = $data[1];
break;
}
}
fclose($handle);
if ($success) {
$_SESSION['allow'] = '';
$_SESSION['status'] = 'Success!';
/*header('Location: ./');*/
} else {
if (empty($_POST['id'])){
$_SESSION['status'] = 'Enter a Driver Pin';
} else {
$_SESSION['status'] = 'Invalid Driver Pin - Try again';
}
}
} else {
$_SESSION['status'] = 'Enter a Driver Pin';
}
?>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
</head>
<body>
<h1>Page Title</h1>
<div class="smallcontainer">
<form method="post" action="">
<div class="row">
<div class="col-20">
<label for="id">Driver Pin</label>
</div>
<div class="col-80">
<input type="text" name="id" >
</div>
</div>
<div class="row">
<div class="col-100 error">
<?= #$_SESSION['status'] ?>
</div>
<div class="col-100">
<input type="submit" value="Submit" name="submit">
</div>
</div>
</form>
</div>
</body>
</html>
This will work - notice I added an else if there is no post on fresh load you need to reset the $_SESSION['status'];

You php that checks and set status is located after HTML, which means the HTML returned by the page submission will not have the status updated until next submission.
So, move your PHP code above HTML.
Also, you can't use header() after anything was printed out already.

Related

fgets() returns false everytime php

my project's task is simple: read from a file line by line, check if the user and password are stored into "database.txt".
the problem is that "fgets($fd)" returns everytime "FALSE" instead of the string, so when i use strpos or substr the return value is always false.
this is my code, thanks for the help:
<?php
function signin(){
if($_SERVER['REQUEST_METHOD']=="POST"){
if(empty($_REQUEST['loginuser']) || empty($_REQUEST['loginpsw']))
return false;
else{
$fd = fopen("database.txt","r");
while(!feof($fd)){
$line = fgets($fd);
if(strpos($line,$_REQUEST['loginuser']) !== false){
if(substr($line,strrpos($line,",")+1) === sha1($_REQUEST['loginpsw'])){
fclose($fd);
return true;
}
}
}
fclose($fd);
return false;
}
}
else return false;
}
if(!signin()){
header("Location: index.html");
}
else {
header("Location: home.php");
}
?>
<html>
<head>
<title>Test Page</title>
<meta name="keywords" content="testpage test page keywords aristocats pompelmi cavolfiore" />
<meta name="author" content ="Alessandro Savona" />
<link rel="stylesheet" type="text/css" href="css/Style.css" />
<link href="https://fonts.googleapis.com/css?family=Bangers" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet">
</head>
<body>
<div class="topbar">
<div class="logo">
<label>We Like Pepperoni</label>
</label>
</div>
<div class="logo2">
<img src="https://www.trieste.pizza/resources/images/trieste-pizza-logo.png">
</div>
</div>
</body>
</html>
again, thank you for the help!
EDIT:
the value of $fd is 5
it's located in the path /var/www/html
that is the file:
###########################################################
# name surname,username,password SHA1(Rowling) #
###########################################################
Harry Potter,potter,f5043f5cb1f52071e96a44303919b88179eb7bad
Ron Weasley,weasley,f5043f5cb1f52071e96a44303919b88179eb7bad
Hermione Granger,granger,f5043f5cb1f52071e96a44303919b88179eb7bad
Lord Voldemort,voldemort,f5043f5cb1f52071e96a44303919b88179eb7bad
Albus Dumbledore,dumbledore,f5043f5cb1f52071e96a44303919b88179eb7bad
Severus Snape,snape,f5043f5cb1f52071e96a44303919b88179eb7bad
Rubeus Hagrid,hagrid,f5043f5cb1f52071e96a44303919b88179eb7bad
Draco Malfoy,malfoy,f5043f5cb1f52071e96a44303919b88179eb7bad

Bulk Email Validator works but not properly

I wish to either make use of a freely available email validator (software, script or online service). There's work but when I insert more than one email at that time only one email verifies which I insert last and other email does not verify. Only one email verifies. How can I do it?
My code:
<?php
include_once('config.php');
if(isset($_POST["email_verify_btn"])) {
$email = $_POST["email_verify"];
if(strpos($email,"\n")) {
$text = explode("\n",$email);
} else {
$text = array($email);
}
function domain_exists($text)
{
$domain = substr(strrchr($text, "#"), 1);
$arr = #dns_get_record($domain, DNS_MX);
if ($arr[0]['host'] == $domain && !empty($arr[0]['target'])) {
return $arr[0]['target'];
}
}
foreach ($text as $abc) {
$domain = "";
$target = "";
$target_ip = "";
if(filter_var($abc, FILTER_VALIDATE_EMAIL)) {
if(domain_exists($abc)){
$domain = substr(strrchr($abc, "#"), 1);
$data = #dns_get_record($domain, DNS_MX);
if (is_array($data) || is_object($data)) {
foreach ($data as $key1) {
$host = $key1['host'];
$target = $key1['target'];
$target_ip = gethostbyname($key1['target']);
}
}
if($data) {
$status = "email id is valid";
}
} else {
$status = "mx recored not exist";
}
} else {
$status = "not in syntax";
}
$c_by = 1;
$c_date = date('Y-m-d H:i:s');
$c_ip = $_SERVER['REMOTE_ADDR'];
$insert = $connect->query("INSERT INTO `email_verify_list`(`primary_email`,`host_name`,`target`,`target_ip`,`session`,`status`,`created_by`, `created_date`, `created_ip`) VALUES ('$abc','$domain','$target','$target_ip','$c_date','$status','$c_by','$c_date','$c_ip')");
if($insert == 1) {
$_SESSION['date_time'] = $c_date;
} else {
echo "<script>alert('!!!!!not successfully');</script>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta charset="utf-8"/>
<title>BULK EMAIL VARIFIER</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"/>
<link rel="apple-touch-icon" href="pages/ico/60.png">
<link rel="apple-touch-icon" sizes="76x76" href="pages/ico/76.png">
<link rel="apple-touch-icon" sizes="120x120" href="pages/ico/120.png">
<link rel="apple-touch-icon" sizes="152x152" href="pages/ico/152.png">
<link rel="icon" type="image/x-icon" href="favicon.ico"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<meta content="" name="description"/>
<meta content="" name="author"/>
<link href="assets/plugins/pace/pace-theme-flash.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/bootstrapv3/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/jquery-scrollbar/jquery.scrollbar.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="assets/plugins/switchery/css/switchery.min.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="pages/css/pages-icons.css" rel="stylesheet" type="text/css" />
<link class="main-stylesheet" href="pages/css/pages.css" rel="stylesheet" type="text/css"/>
<!--[if lte IE 9]>
<link href="assets/plugins/codrops-dialogFx/dialog.ie.css" rel="stylesheet" type="text/css" media="screen" />
<![endif]-->
</head>
<body class="fixed-header ">
<div id="rootwizard" class="m-t-50">
<ul class="nav nav-tabs nav-tabs-linetriangle nav-tabs-separator nav-stack-sm">
<li class="active">
<a data-toggle="tab" href="#tab1"><span>EMAIL VERIFY</span></a>
</li>
</ul>
<form method="post" action="" >
<div class="tab-content">
<div class="tab-pane padding-20 active slide-left" id="tab1">
<div class="row row-same-height">
<div class="col-md-12">
<div class="padding-30">
<div class="row clearfix">
<div class="col-sm-3">
<div class="form-group form-group-default">
<label><font size="2">ENTER YOUR EMAIL <span class="glyphicon glyphicon-envelope"></span></font></label><br>
<textarea cols="43" rows="9" name="email_verify" style="border-color:white;border-width:thin;padding:4pt;" multiple/></textarea><br>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-3">
<button type="submit" name="email_verify_btn" class="btn btn-lg btn-info" style="padding:11pt;width:200px;"/><span><b><font size="2">SUBMIT</font></b></span></button>
</div>
</div> <br><br>
<div class="row clearfix">
<div class="col-sm-12">
<table width="100%" class="table ">
<tr class="success">
<th>PRIMARY EMAIL <span class="glyphicon glyphicon-envelope"></span></th>
<th>HOST NAME</th>
<th>TARGET</th>
<th>TARGET IP</th>
<th>STATUS</th>
</tr>
<?php
$select = $connect->query("SELECT * from `email_verify_list` where session='".$_SESSION['date_time']."' order by id");
while($row = $select->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['primary_email']; ?></td>
<td><?php echo $row['host_name']; ?></td>
<td><?php echo $row['target']; ?></td>
<td><?php echo $row['target_ip']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<script src="assets/plugins/pace/pace.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="assets/plugins/modernizr.custom.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrapv3/js/bootstrap.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery/jquery-easy.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-unveil/jquery.unveil.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-bez/jquery.bez.min.js"></script>
<script src="assets/plugins/jquery-ios-list/jquery.ioslist.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-actual/jquery.actual.min.js"></script>
<script src="assets/plugins/jquery-scrollbar/jquery.scrollbar.min.js"></script>
<script type="text/javascript" src="assets/plugins/classie/classie.js"></script>
<script src="assets/plugins/switchery/js/switchery.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrap3-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script type="text/javascript" src="assets/plugins/jquery-autonumeric/autoNumeric.js"></script>
<script type="text/javascript" src="assets/plugins/bootstrap-tag/bootstrap-tagsinput.min.js"></script>
<script type="text/javascript" src="assets/plugins/jquery-inputmask/jquery.inputmask.min.js"></script>
<script src="assets/plugins/bootstrap-form-wizard/js/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js" type="text/javascript"></script>
<script src="assets/plugins/summernote/js/summernote.min.js" type="text/javascript"></script>
<script src="assets/plugins/moment/moment.min.js"></script>
<script src="assets/plugins/bootstrap-daterangepicker/daterangepicker.js"></script>
<script src="assets/plugins/bootstrap-timepicker/bootstrap-timepicker.min.js"></script>
<script src="pages/js/pages.min.js"></script>
<script src="assets/js/form_wizard.js" type="text/javascript"></script>
<script src="assets/js/scripts.js" type="text/javascript"></script>
<script src="assets/js/demo.js" type="text/javascript"></script>
<script>
window.intercomSettings = {
app_id: "xt5z6ibr"
};
</script>
</body>
</html>
I think , you are not using strpos function in correct way.
you can use this way . Because strpos return also 0 value if any string found at zero position . and return FALSE if search string not found
Also note that string positions start at 0, and not 1.
if(strpos($email,"\n")!==false) {
$text = explode("\n",$email);
} else {
$text = array($email);
}

Log users ip on login PHP mysql

I want to log the users IP when they login all I want it to do it update a column
I know $_SERVER['REMOTE_ADDR']; get their ip.
I want to log it on login on their username row in mysql.
Here's an image of my mysql table
https://gyazo.com/cf5b223df03d0da8a15bf61ed037d847
LoginCheck.php:
<?php
# Processes
function cleanString($con, $string) {
return mysqli_real_escape_string($con, stripcslashes($string));
}
# buttons use the request method
if (isset($_REQUEST['login'])) {
$username = strtolower(cleanString($con, $_POST['username']));
$password = cleanString($con, $_POST['password']);
$errors = array();
if (empty($username) || empty($password)) {
# If they left the shit blank like a jew
$errors[] = "Please make sure you entered a valid username and password";
}
$password = md5($password);
$db_check_username = mysqli_query($con, "SELECT username FROM users WHERE username='$username' OR email='$username'");
$db_check_userdata = mysqli_query($con, "SELECT username,password FROM users WHERE username='$username' AND password='$password' OR email='$username' AND password='$password'");
if (!$db_check_username || !$db_check_userdata) {
$errors[] = mysqli_error($con);
}
if (mysqli_num_rows($db_check_username) == 0) {
# If the username doesn't exist like a bitch
$errors[] = "No account could be found with that username.";
}
if (mysqli_num_rows($db_check_userdata) == 0) {
# If the Username and Password don't match
$errors[] = "Username and Password combination incorrect.";
}
if(empty($errors)) {
session_start();
$_SESSION['username'] = $username;
$success[] = "You have successfully logged in. Redirecting in a moment.";
echo '<meta http-equiv="refresh" content="5; url=index.php" />';
} else {
$danger = $errors;
}
}
?>
Login.php
<?php
# Include da files mate
include('includes/config.php');
include('includes/logincheck.php');
# Other Shit
//nothing yet
?>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<head>
<meta charset="utf-8" />
<title>Twisted Movies | Login Page</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
<meta content="" name="description" />
<meta content="" name="author" />
<!-- ================== BEGIN BASE CSS STYLE ================== -->
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
<link href="assets/plugins/jquery-ui/themes/base/minified/jquery-ui.min.css" rel="stylesheet" />
<link href="assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<link href="assets/css/animate.min.css" rel="stylesheet" />
<link href="assets/css/style.min.css" rel="stylesheet" />
<link href="assets/css/style-responsive.min.css" rel="stylesheet" />
<link href="assets/css/theme/red.css" rel="stylesheet" id="theme" />
<!-- ================== END BASE CSS STYLE ================== -->
<!-- ================== BEGIN BASE JS ================== -->
<script src="assets/plugins/pace/pace.min.js"></script>
<!-- ================== END BASE JS ================== -->
</head>
<body class="pace-top">
<!-- begin #page-loader -->
<div id="page-loader" class="fade in"><span class="spinner"></span></div>
<!-- end #page-loader -->
<div class="login-cover">
<div class="login-cover-image"><img src="assets/img/login-bg/bg-1.jpg" data-id="login-cover-image" alt="" /></div>
<div class="login-cover-bg"></div>
</div>
<!-- begin #page-container -->
<div id="page-container" class="fade">
<!-- begin login -->
<div class="login login-v2" data-pageload-addclass="animated fadeIn">
<!-- begin brand -->
<div class="login-header">
<div class="brand">
<span class="logo"></span> Twisted Movies
<small>Where the best movies are AD free!</small>
</div>
<div class="icon">
<i class="fa fa-sign-in"></i>
</div>
</div>
<!-- end brand -->
<div class="login-content">
<form action="" method="POST" class="margin-bottom-0">
<div class="text-center">
<?php
if (!empty($success)) {
foreach ($success as $value) {
echo '<div class="alert alert-success">';
echo $value.'<br>';
echo '</div>';
}
} elseif (!empty($danger)) {
foreach ($danger as $value) {
echo '<div class="alert alert-danger">';
echo $value.'<br>';
echo '</div>';
}
} elseif (!empty($warning)) {
foreach ($warning as $value) {
echo '<div class="alert alert-warning">';
echo $value.'<br>';
echo '</div>';
}
} elseif (!empty($info)) {
foreach ($info as $value) {
echo '<div class="alert alert-info">';
echo $value.'<br>';
echo '</div>';
}
} else {
echo '<div class="alert alert-info">';
echo "Please enter a username and password.";
echo '</div>';
}
?>
</div>
<div class="form-group m-b-20">
<input type="text" name="username" placeholder="Username" class="form-control input-lg"/>
</div>
<div class="form-group m-b-20">
<input type="password" name="password" placeholder="Password" class="form-control input-lg"/>
</div>
<div class="checkbox m-b-20">
<label>
<input type="checkbox" /> Remember Me
</label>
</div>
<div class="login-buttons">
<button type="submit" name="login" class="btn btn-success btn-block btn-lg">Sign me in</button>
</div>
<div class="m-t-20">
Not a member yet? Click here to register.
</div>
<center><?php include 'includes/footer.php'; ?></center>
</form>
</div>
</div>
<!-- end login -->
</div>
<!-- end page container -->
<!-- ================== BEGIN BASE JS ================== -->
<script src="assets/plugins/jquery/jquery-1.9.1.min.js"></script>
<script src="assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
<script src="assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
<script src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<!--[if lt IE 9]>
<script src="assets/crossbrowserjs/html5shiv.js"></script>
<script src="assets/crossbrowserjs/respond.min.js"></script>
<script src="assets/crossbrowserjs/excanvas.min.js"></script>
<![endif]-->
<script src="assets/plugins/jquery-hashchange/jquery.hashchange.min.js"></script>
<script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/plugins/jquery-cookie/jquery.cookie.js"></script>
<!-- ================== END BASE JS ================== -->
<!-- ================== BEGIN PAGE LEVEL JS ================== -->
<script src="assets/js/login-v2.demo.min.js"></script>
<script src="assets/js/apps.min.js"></script>
<!-- ================== END PAGE LEVEL JS ================== -->
<script>
$(document).ready(function() {
App.init(ajax=true);
LoginV2.init();
});
</script>
</body>
</html>
Right after you have session_start():
$ip = $_SERVER['REMOTE_ADDR'];;
$stmt = $con->prepare("UPDATE users SET `IP`=? WHERE username=?");
$stmt->bind_param("ss", $ip, $username);
$stmt->execute();
Some tips:
Try to always use prepared statements when dealing with user data.
use a HTTP class to get the IP. The IP is not always in REMOTE_ADDR, especially if the site is behind a proxy such as Cloudflare

Can't Open A Page on Site PHP

I have three pages on my site index (login page) Home (navigation) Project Creation and Management (informational) now after login there is no issues however when I try going from home to Project Creation and Management it seams like I'm instantly getting redirected back to the home page. It does the same thing via url entry or the navigation from the home page. Here's my code:
index
<!DOCTYPE html>
<?php
session_start();
$username = "admin";
$password = "collins1";
if (isset($_GET['logout'])){
session_destroy();
}
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
header("Location: home.php");
}
if (isset($_POST['username']) && isset($_POST['password'])){
if ($_POST['username'] == $username && $_POST['password'] == $password)
{
$_SESSION['loggedin'] = true;
header("Location: home.php");
}
else
{
echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>';
}
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login</title>
<link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
<link href="../CSS/master.css" rel="stylesheet" type="text/css">
<script src="../JAVASCRIPT/respond.min.js"></script>
</head>
<body link="black">
<div class="gridContainer clearfix">
<div id="borderDiv">
<div id="headerDiv">
<p>Welcome</p>
</div>
<div id="subHeaderDiv">
<p>Please login to continue to the Project Creation and Management System</p>
</div>
<form method="post" action="index.php">
<div id="userNameLoginDiv">
<p>Username:</p>
<input type="text" name="username" size="12">
</div>
<div id="userPasswordLoginDiv">
<p>Password:</p>
<input type="password" name="password" size="12">
</div>
<div id="loginBtnDiv">
<input id="button" type="submit" value="Login">
</div>
</form>
</div>
</div>
</body>
</html>
home
<!DOCTYPE html>
<?php
session_start();
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
<link href="../CSS/master.css" rel="stylesheet" type="text/css">
<script src="../JAVASCRIPT/respond.min.js"></script>
</head>
<body link="black">
<div class="gridContainer clearfix">
<div id="headerDiv">
<p>Home</p>
</div>
<font color="#000000">Logout</font>
<div id="homeBtn1"> <img src="../button.png" alt="Project Creation and Management">
<div id="homeBtnText1">
<font color="#000000" ><p>Project Creation and Management<p></font>
</div>
</div>
</div>
</body>
</html>
Project Creation and Management
<!DOCTYPE html>
<?php
session_start();
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link href="../CSS/boilerplate.css" rel="stylesheet" type="text/css">
<link href="../CSS/master.css" rel="stylesheet" type="text/css">
<script src="../JAVASCRIPT/respond.min.js"></script>
</head>
<body link="black">
<div class="gridContainer clearfix">
<div id="headerDiv">
<p>Project Creation & Management</p>
</div>
<font color="#000000">Logout</font>
</div>
</body>
</html>
you should edit your code below
<?php
session_start();
$username = "admin";
$password = "collins1";
if (isset($_GET['logout'])){
session_destroy();
}
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
header("Location: home.php");
}
if (isset($_POST['username']) && isset($_POST['password'])){
if ($_POST['username'] == $username && $_POST['password'] == $password)
{
$_SESSION['loggedin'] = true;
header("Location: home.php");
}
else
{
echo '<font color="#FF0000"><p align="center">Username or Password incorrect please try again</p></font>';
}
}
?>
...
Because session_start() have to above every output-string. You printed <!DOCTYPE html> that make session cannot start
The session_start(); must go above all else in all pages. Otherwise, the session variables cannot be created and saved. For that reason, the second if in your Project Creation and Management page will be called. That's the problem!

Not getting the reason for the error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
There are quite a few solutions available for this error but none seems to be working for me. I am a new php developer and trying to create web site using php programs.
The error I'm getting is "Warning: Cannot modify header information - headers already sent by (output started at /home/influss/public_html/Influsstest/hiremain.php:39) in /home/influss/public_html/Influsstest/hiremain.php on line 74"
I have checked the line no. 39 and do not find any whitespace before or after the php. also there are no white spaces in the beginning or the end of the program but still I'm getting this error.
the code is as follows:
`
<!doctype html>
<html><!-- InstanceBegin template="/Templates/registrtion.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Hire Main</title>
<!-- InstanceEndEditable -->
<link href="/css/styles-reg.css" rel="stylesheet" type="text/css" media="all">
<link href="/css/reg.css" rel="stylesheet" type="text/css" media="all"/>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,500' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,600,900,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Slabo+13px' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Rokkitt:400,700' rel='stylesheet' type='text/css'>
<!-- InstanceBeginEditable name="head" -->
<link href="css/styles.css" rel="stylesheet" type="text/css" media="all">
<!-- InstanceEndEditable -->
</head>
<body>
<div id="wrapper">
<div id="top">
<div id = "logo">
<img src="images/logo2.jpg">
</div>
<div id = "social-media">
<p>For additional information<br>Call Phone No.</p>
<ul>
<li><img src="/images/icons/facebook1.png"></li>
<li><img src="/images/icons/twitter1.png"></li>
<li><img src="/images/icons/linkedin1.png"></li>
<li><img src="/images/icons/googleplus1.png"></li>
</ul>
</div>
</div>
<!-- <div id="Banner">
</div>
--> <div id="Content-wrapper">
<div id="content">
<!-- InstanceBeginEditable name="content" -->
**<?php**
error_reporting(E_ALL); ini_set('display_errors', 'On');
function renderForm($id, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="margin:0 200px;padding:4px; color:red ;font-weight:bold;">'.$error.'</div>';
}
?>
<form action="hiremain.php" method="post" class="newreg">
<fieldset class="row1">
<p>
<label class="labelstyle">Enter Registration ID: *</label>
<input type="text" name="regid" value="<?php echo $id;?>"/>
</p>
</fieldset>
<p></p>
<input class="button1" type="submit" name="submit" value="Get Details">
</form>
<p></p>
<?php
}
?>
<?php
include('connect.php');
if (isset($_POST['submit']))
{
$id = $_POST['regid'];
$sql = "Select * from hirefinal where hr_id = '$id'";
$result = mysql_query($sql) or die(mysql_error());
$total_results = mysql_num_rows($result);
if ($total_results > 0)
{
header('location:hireedit.php?regid='.$id);
}
else
{
$id = $_POST['regid'];
$sql = "Select * from hirefinal where hr_id = '$id'";
$result = mysql_query($sql) or die(mysql_error());
$total_results = mysql_num_rows($result);
if ($total_results > 0)
{
header("location:hireedit.php?regid=" . $id);
}
Else
{
$error = "Please register first";
renderForm($id, $error);
}
}
mysql_free_result($result);
}
else
{
$error = "";
$id="";
renderform($id, $error);
}
?>
Register a Request
Home
<p></p>
<!-- InstanceEndEditable -->
</div>
</div>
<div id="footer">
<p>©Copyright 2014 • All rights Reserved • Influss.com, Chennai, Mob: +9112345-12345, E-mail: abc#influss.com</p>
</div>
</div>
</body>
<!-- InstanceEnd -->
</html>
`
The error happens to be in the line which is marked by *.
Request somebody help me out on this.
You can't send headers after an html output.
You have to put them before
<!doctype html>
You can resolve this using:
<?php
error_reporting(E_ALL); ini_set('display_errors', 'On');
function renderForm($id, $error)
{
if ($error != '')
{
$problems = '<div style="margin:0 200px;padding:4px; color:red ;font-weight:bold;">'.$error.'</div>';
}
}
include('connect.php');
if (isset($_POST['submit']))
{
$id = $_POST['regid'];
$sql = "Select * from hirefinal where hr_id = '$id'";
$result = mysql_query($sql) or die(mysql_error());
$total_results = mysql_num_rows($result);
if ($total_results > 0)
{
header('location:hireedit.php?regid='.$id);
}
else
{
$id = $_POST['regid'];
$sql = "Select * from hirefinal where hr_id = '$id'";
$result = mysql_query($sql) or die(mysql_error());
$total_results = mysql_num_rows($result);
if ($total_results > 0)
{
header("location:hireedit.php?regid=" . $id);
}
Else
{
$error = "Please register first";
renderForm($id, $error);
}
}
mysql_free_result($result);
}
else
{
$error = "";
$id="";
renderform($id, $error);
}
?>
<!doctype html>
<html><!-- InstanceBegin template="/Templates/registrtion.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Hire Main</title>
<!-- InstanceEndEditable -->
<link href="/css/styles-reg.css" rel="stylesheet" type="text/css" media="all">
<link href="/css/reg.css" rel="stylesheet" type="text/css" media="all"/>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,500' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Raleway:400,600,900,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Slabo+13px' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Rokkitt:400,700' rel='stylesheet' type='text/css'>
<!-- InstanceBeginEditable name="head" -->
<link href="css/styles.css" rel="stylesheet" type="text/css" media="all">
<!-- InstanceEndEditable -->
</head>
<body>
<div id="wrapper">
<div id="top">
<div id = "logo">
<img src="images/logo2.jpg">
</div>
<div id = "social-media">
<p>For additional information<br>Call Phone No.</p>
<ul>
<li><img src="/images/icons/facebook1.png"></li>
<li><img src="/images/icons/twitter1.png"></li>
<li><img src="/images/icons/linkedin1.png"></li>
<li><img src="/images/icons/googleplus1.png"></li>
</ul>
</div>
</div>
<!-- <div id="Banner">
</div>
--> <div id="Content-wrapper">
<div id="content">
<!-- InstanceBeginEditable name="content" -->
<?php
echo $problems;
?>
<form action="hiremain.php" method="post" class="newreg">
<fieldset class="row1">
<p>
<label class="labelstyle">Enter Registration ID: *</label>
<input type="text" name="regid" value="<?php echo $id;?>"/>
</p>
</fieldset>
<p></p>
<input class="button1" type="submit" name="submit" value="Get Details">
</form>
<p></p>
Register a Request
Home
<p></p>
<!-- InstanceEndEditable -->
</div>
</div>
<div id="footer">
<p>©Copyright 2014 • All rights Reserved • Influss.com, Chennai, Mob: +9112345-12345, E-mail: abc#influss.com</p>
</div>
</div>
</body>
<!-- InstanceEnd -->
</html>

Categories