Using fopen to write lines of text into a txt file php - php

I'm having a problem with PHP when trying to use the fopen to write lines of text inside of a textarea to a .txt file. Here's my code, hopefully someone can help!
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
$username = $_SESSION['username'];
$file = "extra/" . $username . ".png";
if (!file_exists($file)) $file = 'extra/defaultProfile.png';
function sendData($u) {
$myfile = fopen('bio/' . $username . '.txt', $u);
}
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>Downloads</li>
<li>Chat</li>
<li>Profile</li>
<li class="logout">Logout</li>
</ul>
</nav>
</div>
</header>
<body>
<div class="profileimg">
<img src=<?php echo $file; ?> width="125" height="125">
</div>
<div class="profilename">
<p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
</div>
<div class="biotext">
<form action="" method="post">
<textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
<input type="submit" name="submit" value="submit" formaction="sendData()"/>
</form>
</div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>
I've tried everything that could come up but I can't seem to find the right solution... I tried making and using a function called sendData() but that didn't work either. Any help anyone can provide is greatly appreciated, thanks!

check if the file exist
check if permission is granted to write the file
sendData() its php and you call it from html
you forgot declare this variable = $u when you call the function
you can do something like this:
<?php
if (!empty($_POST))
{
$text_data = $_POST['text_data'];
$username = $_POST['username'];
fopen('bio/' . $username . '.txt', $text_data);
}
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>Downloads</li>
<li>Chat</li>
<li>Profile</li>
<li class="logout">Logout</li>
</ul>
</nav>
</div>
</header>
<body>
<div class="profileimg">
<img src=<?php echo $file; ?> width="125" height="125">
</div>
<div class="profilename">
<p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
</div>
<div class="biotext">
<form action="" method="post">
<textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
<input type="text" name="text_data" value="">
<input type="hidden" name="username" value="<?php echo $username ?>">
<input type="submit" name="submit" value="submit"/>
</form>
</div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>

Related

How to insert dynamically created textboxes with the same name into database?

This is my html code. It dynamically creates these textboxes but they all have the same name.
<?php
include 'db.php';
$office = $_GET['office'];
$application_name = $_GET['application_name'];
?>
<!DOCTYPE html>
<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="">
<title>Add Requirements for New Application</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/sidebar.css" rel="stylesheet">
<script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
<script src="js/jquery.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<!-- Bootstrap css library -->
</head>
<style>
.header--banner {
background-image: url(cb.jpg);
background-size:100% 100%;
background-repeat: no-repeat;
}
.header--banner {
height: 45.5rem;
}
.jumbotron{
padding: 0.5em 0.6em;
}
.header--banner {
background-image: url(magic.jpg);
background-size:100% 100%;
background-repeat: no-repeat;
}
.header--banner {
height: 45.5rem;
}
.jumbotron{
padding: 0.5em 0.6em;
position: relative;
text-align: center;
color: black;
}
.green{
text-decoration: none;
font-family: Modern;
font-size: 18px;
border-radius: 5px;
padding: 5px 8px;
background-color: #27ae60;
color: #ffffff;
}
a.green:hover{
text-decoration: none;
background-color: #145a32;
color: #ffffff;
transition: 0.7s;
}
.bs-example2{
border: 3px solid black;
background: #f5f5f5;
text-align: center;
padding:20px;
margin:5px;
height:200px;
}
</style>
<body>
<header class="header--banner">
<div id="wrapper">
<!-- Sidebar -->
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li class="sidebar-brand">
<a href="kiosk-HOMEPAGE.php">
HOME
</a>
</li>
<li>
Offices
</li>
<li>
Employees
</li>
<li>
Change Username & Password
</li>
<li>
Reports
</li>
<li>
Log Out
</li>
</ul>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="jumbotron">
<div class="container-fluid">
<img src="urdBanner.jpg" id="urdBanner" alt="Banner" style="width:100%; height:250px;">
<center><h2 style="position:relative;">Requirements</h2></center>
<p></p>
<img src="menu.jpg" class="rounded-circle" style="width:30px; height:30px;">
<br>
<br>
Previous
<br>
<br>
<form method="get" action="kiosk-NEW_CHARTER_REQUIREMENTS_insert.php">
<input type="hidden" name="office" value="<?php echo $office ?>">
<input type="hidden" name="application_name" value="<?php echo $application_name ?>">
<br>
<div class="row form-group">
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Step By Step Procedure</b></label>
</div>
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Personnel in charge</b></label>
</div>
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Time needed to complete procedure</b></label>
</div>
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Amount of fees if any</b></label>
</div>
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Required Documents</b></label>
</div>
<div class="col-md-2"><label for="textBox" class="col-form-label"><b>Procedure for the filing of complaints</b></label>
</div>
</div>
</div>
<div class="form-group fieldGroup">
<div class="input-group">
<div class="col-md-2">
<textarea name="steps[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Steps"></textarea>
</div>
<div class="col-md-2">
<textarea name="personnel[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Personnel"></textarea>
</div>
<div class="col-md-2">
<textarea name="time[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Time"></textarea>
</div>
<div class="col-md-2">
<textarea name="fees[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Fees"></textarea>
</div>
<div class="col-md-2">
<textarea name="documents[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Documents"></textarea>
</div>
<div class="col-md-2">
<textarea name="complaints[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Complaints"></textarea>
</div>
<div class="input-group-addon">
<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add
</div>
</div>
</div>
<!-- copy of input fields group -->
<div class="form-group fieldGroupCopy" style="display: none;">
<div class="input-group">
<div class="col-md-2">
<textarea name="steps[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Steps"></textarea>
</div>
<div class="col-md-2">
<textarea name="personnel[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Personnel"></textarea>
</div>
<div class="col-md-2">
<textarea name="time[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Time"></textarea>
</div>
<div class="col-md-2">
<textarea name="fees[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Fees"></textarea>
</div>
<div class="col-md-2">
<textarea name="documents[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Documents"></textarea>
</div>
<div class="col-md-2">
<textarea name="complaints[]" class="form-control" onkeyup="AutoGrowTextArea(this)" placeholder="Complaints"></textarea>
</div>
<div class="input-group-addon">
<span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Remove
</div>
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT"/>
</form>
</div>
</div>
</div>
</div>
</header>
<script type="text/javascript">
$(document).ready(function(){
//group add limit
var maxGroup = 10;
//add more fields group
$(".addMore").click(function(){
if($('body').find('.fieldGroup').length < maxGroup){
var fieldHTML = '<div class="form-group fieldGroup">'+$(".fieldGroupCopy").html()+'</div>';
$('body').find('.fieldGroup:last').after(fieldHTML);
}else{
alert('Maximum '+maxGroup+' groups are allowed.');
}
});
//remove fields group
$("body").on("click",".remove",function(){
$(this).parents(".fieldGroup").remove();
});
});
</script>
<script type="text/javascript">
// Auto-Grow-TextArea script.
// Script copyright (C) 2011 www.cryer.co.uk.
// Script is free to use provided this copyright header is included.
function AutoGrowTextArea(textField)
{
if (textField.clientHeight < textField.scrollHeight)
{
textField.style.height = textField.scrollHeight + "px";
if (textField.clientHeight < textField.scrollHeight)
{
textField.style.height =
(textField.scrollHeight * 2 - textField.clientHeight) + "px";
}
}
}
</script>
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Menu Toggle Script -->
<script>
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
</script>
</body>
</html>
this is my insert.php
<?php
if(isset($_GET['submit'])){
$con = mysqli_connect('localhost','root','','information_kiosk');
$office = $_GET['office'];
$application_name = $_GET['application_name'];
$steps = $_GET['steps'];
$personnel = $_GET['personnel'];
$time = $_GET['time'];
$fees = $_GET['fees'];
$documents = $_GET['documents'];
$complaints = $_GET['complaints'];
$totalSize = sizeof($steps);
for($i=0;$i <=$totalSize;$i++) {
$InsertSteps = (!empty($steps[$i])) ? $steps[$i] : '';
$InsertPersonnel = (!empty($personnel[$i])) ? $personnel[$i] : '';
$InsertTime = (!empty($time[$i])) ? $time[$i] : '';
$InsertFees = (!empty($fees[$i])) ? $fees[$i] : '';
$InsertDocuments = (!empty($documents[$i])) ? $documents[$i] : '';
$InsertComplaints = (!empty($complaints[$i])) ? $complaints[$i] : '';;
echo $InsertSteps;
echo "<br>";
echo $InsertPersonnel;
echo $InsertSteps;
echo "<br>";
}
}
?>
*update. with my insert code now it only shows the first row of data but not the 2nd or 3rd
only the first row gets inserted into database but the following dynamically created rows dont. My question is how could i change my syntax so that each row would insert into the database for example.
stepsColumn PersonnelColumn timeColumn feesColumn documentsColumn
array[0] array[0] array [0] array[0] array[0]
array[1] array[1] array [1] array[1] array[1]

how to fetch and display the data one by one by clicking next button

I have a quiz management system and need to fetch the data from the database and displaying the data one by one on clicking next button.
I have used ajax to insert data into database after clicking next button by using file read and write function.
I am hoping for a better and efficient solution for my problem.
<?php
$host="localhost";
$user="root";
$pass="";
$con=mysqli_connect($host,$user,$pass);//connect to database;
mysqli_select_db($con,'*****');
// Create connection
//$conn = new mysqli($host, $user, $pass);
?>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
nav {
background-color: #2196F3 !important;
}
.second {
background-color: #61BAE4 !important;
height:45px;
}
.logo {
display: inline-block;
height: 100%;
}
.logo>img {
vertical-align: middle
padding-bottom: 10px;
}
#media only screen and (max-device-width: 480px) {
#media only screen and (max-device-width: 480px) {
.card-panel{
width:280px !important;
}
nav{
width:300px !important;
}
</style>
</head>
<body style="background-color: #969696;">
<div class="row">
<nav>
<div class="nav-wrapper">
<img width="60" height="60" src="https://www.timefortest.com/images/logo1.png" alt=""><span>IBPS Portal</span>
<i class="material-icons">menu</i>
<ul class="right hide-on-med-and-down">
<li>Dashboard</li>
<li>Candidates</li>
<li>Add Question</li>
<li><a id="logout" href="logout">Logout</a></li>
<li><a id="logout" href="logout"><i class="material-icons">lock_outline</i></a></li>
</ul>
<!--<ul class="side-nav" id="mobile-demo">
<li>Dashboard</li>
<li>Candidates</li>
<li>Add Question</li>
<li><a id="logout" href="logout">Logout</a></li>
<li><i class="material-icons">lock_outline</i></li>
</ul>-->
</div>
</nav>
</div>
<!--<nav class="second">
<div class="nav-wrapper">
<div class="col s12">
Dashboard
Add Questions
Multiple Choice
</div>
</div>
</nav>-->
<?php $var=0;?><?php
//<div id="response">
?><form method="POST" action="submitanswer.php" id="answerdata">
<?php
$myfile = fopen("hitcount.txt", "r") or die("Unable to open file!");
$fh=fread($myfile,filesize("hitcount.txt"));
fclose($myfile);
$sql="SELECT description,question,choiceA,choiceB,choiceC,choiceD,choiceE,msqid FROM master_question where msqid=$fh";
if($r=mysqli_query($con,$sql))
{
while($result= $r->fetch_row())
{
?>
<div class="col s12 m12"style="background-color:Orange;">
<div class="row">
<div class="col s6 m6">
<h4 style="color:white;">DESCRIPTION</h4>
<div class="card horizontal" style="height:500px;background-color:white;">
<div class="card-stacked">
<div class="card-content">
<h5> <?php echo $result[0]?> </h4>
</div>
</div>
</div>
</div>
<div class="col s6 m6">
<h4 style="color:white;">QUESTION</h4>
<div class="card horizontal"style="height:500px;background-color:white;">
<div class="card-stacked">
<div class="card-content">
<h5><?php echo $result[1]?></h4>
</br />
<div class="card-stacked"style="padding-top:40px;">
<div class="card-content">
<input type="hidden" name="msqid" value="<?php echo $result[7]?>"/>
<p>
<input type="radio" name="group" id="test1" name="test1" value="<?php echo $result[1]?>" />
<label for="test1"><Strong> <?php echo $result[2]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test2" name="test2" value="<?php echo $result[2]?>" />
<label for="test2"><Strong><?php echo $result[3]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test3" name="test3" value="<?php echo $result[3]?>" />
<label for="test3"><Strong><?php echo $result[4]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test4" name="test4" value="<?php echo $result[4]?>" />
<label for="test4"><Strong><?php echo $result[5]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test5" name="test5" value="<?php echo $result[5]?>" />
<label for="test5"><Strong><?php echo $result[6]?></strong></label>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php } } ?>
<div class="col s12 m12">
<div class="row">
<div class="col s6 m6">
<center>
<input type='button'value='back' class="btn" onclick="document.location.href='decrement.php';"/>
</center>
</div>
<div class="col s6 m6 offset-s3">
<center>
<button type="submit" id="submcq" value="Submit" name="Snext" class="btn btn-success">Next</button></center>
</center>
</div>
</div>
</div>
</div>
</form>
</div>
<script type="text/javascript" src="js/materialize.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(".button-collapse").sideNav();<!--nav jquery starts-->
$(document).ready(function() {
Materialize.updateTextFields();
$(".button-collapse").sideNav();
$('select').material_select();
});
</script>
<script>
function copy() {
document.getElementById("label").innerHTML = document.getElementById("mySelect").value
}<!--to label the selected value-->
$(document).ready(function() {
$('select').material_select();
}); </script>
<script><!--ajax function to insert answer in answer table-->
$(document).ready(function(){
$("answerdata").on('submit',(function (e){
e.preventDefault();
$.ajax({
type:'POST',
url:'submitanswer.php',
data:$('#answerdata').serialize(),
contentType:true,
cache:true,
processData:true,
success:function(){
alert("worked");
}
});
}));
return false;
});
</script>
</body>
</html>
Insert page:
<?php
$host="localhost";
$user="root";
$pass="";
$con=mysqli_connect($host,$user,$pass);//connect to database;
mysqli_select_db($con,'******');
?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$msqid=$_POST['msqid'];
$answer=$_POST['group'];
$sql="INSERT INTO review(question_attend,Question_id,answerA,answerB,answerC,answerD,answerE)VALUES('','$msqid','$answer','','','','')";
$query= mysqli_query($con,$sql);
if($query)
{
echo "success";
$fp = fopen('hitcount.txt', 'c+');
flock($fp, LOCK_EX);
$count = (int)fread($fp, filesize('hitcount.txt'));
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, $count + 1);
flock($fp, LOCK_UN);
fclose($fp);
header("Location: newfetch.php");
}
else
{
echo mysqli_error;
}
}
?>

Why my 2 account is log in at the same time?

I have 2 log in, one for admin table and the other one is for cashier table. So what is happening right now is whenever I log in either one of them then the other one is also log when I refresh it. Same goes when I log out when I click log out in admin then refresh the cashier the cashier is also log out. I dont
know whats wrong in my codes.
P.S. The two table is in one database only. But I dont think its need 2 database for 2 log in.
This is the code index.php for admin_tbl
<?php
/** *index.php **/
session_unset();
session_start();
require_once 'includes/database.php';
$error = '';
if (isset($_POST['login']))
{
$uname = $_POST['username'];
$pword = $_POST['password'];
$sql = "SELECT * FROM admin_tbl WHERE uname='$uname' AND pword='$pword' LIMIT 1";
$result = mysqli_query(connection(), $sql);
$user = mysqli_fetch_array($result, MYSQLI_BOTH);
if ($user)
{
$_SESSION['user_id'] = $user['id']; $_SESSION['name'] = $user['lastname'] . ', ' . $user['firstname'];
header("Location: user_maintenance.php");
}
else
echo '<script language="javascript">';
echo 'alert("Account does not exist!")';
echo '</script>';
}
if (isset($_SESSION['user_id']))
{
header("Location: user_maintenance.php");
}
?>
<html>
<head>
<title>Log In</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <link href="css/bootstrap.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
body {
background:url(images/jerica.jpg)fixed no-repeat center;
background-size:cover;
font-family: 'helvetica','Source Sans Pro', sans-serif;
color: ;
font-weight: 300;
padding-top: 110px;
}
.navbar {
background-color: #0A3D73;
}
.navbar-brand {
height: 100px;
line-height: 70px;
color: #fff !important;
}
.footer {
width: 100%;
position: absolute;
bottom: 20px;
}
.newtext{
color: grey;
}
#media (max-width: 367px){
.login-form {
margin-top: 5px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-4 well login-form" style="margin-top:40px; background:rgba(0,0,0,0.65);"> <form action="" method="post" class="form-horizontal" role="form">
<h1 class = "newtext"><center>Login</center></h1>
<?php if ($error != ''): ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span></button>
<?= $error ?>
</div>
<?php endif; ?>
<div class="form-group col-sm-12"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" name="username" class="form-control" placeholder="Username" required>
</div>
</div>
<div class="form-group col-sm-12">
<div class="input-group" style="padding-bottom: 10px"> <span class="input-group-addon"> <i class="glyphicon glyphicon-lock"></i></span>
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
</div>
<input type="hidden" name="student" value="student">
<div class="form-inline">
<center><button class="btn btn-primary" type="submit" name="login"> Log In</button>
</form>
</div>
</div>
</div>
</body>
</html>
And this is the code of index.php for cashier_tbl
<?php
/** *index.php **/
session_unset();
session_start();
require_once 'includes/database.php';
$error = '';
if (isset($_POST['login']))
{
$uname = $_POST['username'];
$pword = $_POST['password'];
$sql = "SELECT * FROM cashier_tbl WHERE uname='$uname' AND pword='$pword' LIMIT 1";
$result = mysqli_query(connection2(), $sql);
$user = mysqli_fetch_array($result, MYSQLI_BOTH);
if ($user)
{
$_SESSION['user_id'] = $user['id']; $_SESSION['name'] = $user['lastname'] . ', ' . $user['firstname'] . ' ' . $user['middlename'];
header("Location: order.php");
}
else
echo '<script language="javascript">';
echo 'alert("Account does not exist!")';
echo '</script>';
}
if (isset($_SESSION['user_id']))
{
header("Location: order.php");
}
?>
<html>
<head>
<title>Log In</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <link href="css/bootstrap.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
body {
background:url(images/jerica.jpg)fixed no-repeat center;
background-size:cover;
font-family: 'helvetica','Source Sans Pro', sans-serif;
color: ;
font-weight: 300;
padding-top: 110px;
}
.navbar {
background-color: #0A3D73;
}
.navbar-brand {
height: 100px;
line-height: 70px;
color: #fff !important;
}
.footer {
width: 100%;
position: absolute;
bottom: 20px;
}
.newtext{
color: grey;
}
#media (max-width: 367px){
.login-form {
margin-top: 5px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-4 well login-form" style="margin-top:40px; background:rgba(0,0,0,0.65);"> <form action="" method="post" class="form-horizontal" role="form">
<h1 class = "newtext"><center>Login</center></h1>
<?php if ($error != ''): ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span></button>
<?= $error ?>
</div>
<?php endif; ?>
<div class="form-group col-sm-12"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" name="username" class="form-control" placeholder="Username" required>
</div>
</div>
<div class="form-group col-sm-12">
<div class="input-group" style="padding-bottom: 10px"> <span class="input-group-addon"> <i class="glyphicon glyphicon-lock"></i></span>
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
</div>
<input type="hidden" name="student" value="student">
<div class="form-inline">
<center><button class="btn btn-primary" type="submit" name="login"> Log In</button>
</form>
</div>
</div>
</div>
</body>
</html>
UPDATE: Thanks guys! It helps me.
Use a session variable for users and one for cashier. So
$_SESSION['admin_logged_in']
and
$_SESSION['cashier_logged_in']
or something...
(Also, read this http://php.net/manual/en/security.database.sql-injection.php)
Because you're using the same session variable.
Use different session variables for different logins.

Page redirects to itself

I am trying to add an user for the login page but it keeps redirecting even though there is no redirection provided in the code
login.php
<?php
include './connect.php';
session_start();
if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST['username'])) && (!empty($_POST['password']))) {
$postedUsername = $_POST['username'];
$postedPassword = $_POST['password'];
$userDatabaseFind = $database->login->findOne(array('username' => $postedUsername, 'password' => $postedPassword));
$storedUsername = $userDatabaseFind['username'];
$storedPassword = $userDatabaseFind['password'];
if (($postedUsername == $storedUsername) && ($postedPassword == $storedPassword)) {
$_SESSION['username'] = $storedUsername;
if($_SESSION['username'] == 'admin'){
session_regenerate_id();
header('location:printDetail.php');
}
else{
session_regenerate_id();
header('location:welcome.php');
}
} else {
echo 'Error';
}
}
printDetail.php
<?php
require './connect.php';
require './login.php';
//SEARCHING ACROSS THE COLLECTIONS IN THE DATABASE
$printDetailCollections = $database->details;
$printDetailCursor = $printDetailCollections->find();
//INITIALIZE THE VALUE TO ZERO SO IT CAN GO THROUGH THE DATABASE
$i = 0;
//USERNAME AND PASSWORD ARE CHECKED IF IT IS ASSIGNRD THEN THE SESSION IS DISPLAYED
if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {
//LOOP FOR TRAVERSING ACROSS THE DATABASE
foreach ($printDetailCursor as $doc) {
$i++;
}
} else {
header('location:index.html');
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
body{
background-image: url(images/16386858141_65a65879cd_b.jpg);
background-repeat: no-repeat;
background-size: cover;
}
</style>
<script>
$(document).ready(function () {
window.onload = $('#table').hide();
});
</script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Business card Management</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Add entry</li>
<li>Delete entry</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-log-out"></span> Log out</li>
</ul>
</div>
</nav>
<div class="container">
<table id="table">
<thead>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Company Name</th>
<th>Designation</th>
</tr>
</thead>
<tbody>
<?php
foreach ($printDetailCursor as $printDetailDocument) {
?>
<tr>
<td>
<?php
echo (json_decode($printDetailDocument['First Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Second Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Company Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Designation']));
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
I have 3 collections in the mongodb login and details.The add user tries to access the login collection for adding the user
addUser.php
<?php
include './connect.php';
$username = NULL;
$password = NULL;
$confPassword = NULL;
$passwordError = "";
$userEntryCollection = NULL;
if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userEntryCollection = $database->login;
if (isset($_POST['username'])) {
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['confPassword'])) {
$confPassword = $_POST['confPassword'];
}
} else {
echo 'error';
}
if ($password != $confPassword) {
$passwordError = "Your passwords doesnot match";
} else {
$userEntry = array(
"username" => $username,
"password" => $password,
);
$userEntryCollection->insert($userEntry);
}
}
?>
<html>
<head>
<link rel="stylesheet" href=bootstrap-3.3.7-dist/css/bootstrap.min.css>
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<style>
.col-lg-10{
position: relative;
width: 350px;
border-radius: 25px;
}
form{
position: absolute;
top: 10%;
left: 40%;
}
body{
background-image: url(images/16386858141_65a65879cd_b.jpg);
background-repeat: no-repeat;
background-size: cover;
}
form{
color: black;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav navbar-left">
<li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
</ul>
</nav>
<form method="POST" class="col-lg-10" <?php echo $passwordError; ?> >
<div class="form-group form-inline"><br>
<label for="firstName">Username</label><br>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group form-inline">
<label for="secondName">Password:</label><br>
<input type=password class="form-control" id="password" name="password">
</div>
<div class="form-group form-inline">
<label for="secondName">Confirm Password</label><br>
<input type=password class="form-control" id="confPassword" name="confPassword">
</div>
<button type="button" class="btn btn-success">Submit</button>
</form>
</body>
</html>
i have tried to disable access to the printDetail.php via the URL
and i think that is the main reason for redirection.But couldn't figure out the error in the code
The page gets redirected just by clicking the field of the Username
P.s: i know the password stored this way is insecure
I tryed your code on my server and I confirm your page redirection problem when trying to input something in the addUser.php form...
Your mistake is in these lines:
<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav navbar-left">
<li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
</ul>
</nav>
You didn't close the ancor for "Back".
The result is all your form and every single elements of the pages (until another ancor... But there is none) are included by the browser in this ancor. Then also are "Back" links.
It is simple as this.
Use [F12] to debug via the console and code inspector, next time...
;)

php code block not working on submit button

I have the following codes written:
<html>
<head>
<?php
if (isset($_POST['report']))
{
$name = $_POST['Name'] ;
$email = $_POST['Email'] ;
header('Location: subscriber.php?key='.$name.'?key1='.$email);
}
?>
<script type="text/javascript" src="assets/bootstrap/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="inner.css" type="text/css" />
<style>
div#copyright {
float: left;
width: 100%;
background: #000;
color: #fff;
padding: 15px 15px;
}
.c_right {
float: left;
width: 50%;
text-align: left;
}
#copyright li {
float: left;
width: 30px;
margin-right: 10px;
list-style:none !important;
}
.c_left {
float: right;
}
</style>
</head>
<body>
<div id="footer">
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 col-sm-3">
<h3>Quick Links</h3>
<div id="footnav1">
<ul>
<li>About Us</li>
<li>FAQs</li>
<li>Sitemap</li>
<li>Contact Us</li>
<li>Term & Conditions</li>
</ul>
</div>
</div>
<div class="col-xs-6 col-sm-3">
<h3>Quick Links</h3>
<div id="footnav2">
<ul>
<li>Privacy & Cookie Policy</li>
<li>Megahung Blog</li>
<li>Donate Cashback</li>
<li>How Megahung Works</li>
<li>Why Choose Megahunt?</li>
</ul>
</div>
</div>
<div class="col-xs-6 col-sm-3">
<h3>Quick Links</h3>
<div id="footnav3">
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Catalog</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</div>
</div>
<div class="col-xs-6 col-sm-3">
<h3>Newsletter</h3>
<div id="footnav4" class="nl_subscribe">
<form method="post">
<input type="text" name="Name" size="20" class="email_css" placeholder="Enter your name...">
<input type="text" name="Email" size="20" class="email_css" placeholder="Enter your email...">
<input type="submit" name="report" value="Subscribe" class="btn btn-subscribe"/>
</form>
</div>
</div>
</div>
</div>
</div>
<div id="copyright">
<div class="c_right"><h3 class="cp">Copyright © 2015 <span style="color:#ff6803">Megahunt</span>. All Rights Reserved.</h3></div>
<div class="c_left">
<ul class="social">
<li><img src="images/facebook.png"></li>
<li><img src="images/twitter.png"></li>
<li><img src="images/linkedin.png"></li>
<li><img src="images/google.png"></li>
<li><img src="images/instagram.png"></li>
<li><img src="images/pinterest.png"></li>
<ul>
</div>
</div>
</body>
</html>
Now when I click on the Submit button the page is loaded but its actually loaded in the same page not going to subscriber.php page.
I have placed the php block before the html also but still the same issue persists.
Can anyone help me with this issue please.
I reopened the question since the OP does not seem to understand what the duplicate question link's instructions are.
How to fix "Headers already sent" error in PHP
As I said in comments, you are outputting before header, and you're not seeing this warning because your system most likely does not have error reporting enabled. Consult my footnotes about this.
Modify your code to this (rest of code omitted) and exit added.
Sidenote: Make sure there are no spaces before your opening <?php tag and that your file does not contain a byte order mark. Absolutely NOTHING should be above your opening PHP, no includes etc.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_POST['report']))
{
$name = $_POST['Name'] ;
$email = $_POST['Email'] ;
header('Location: subscriber.php?key='.$name.'?key1='.$email);
exit;
}
?>
<html>
<head>
<script type="text/javascript" src="assets/bootstrap/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="inner.css" type="text/css" />
...followed by the rest of your code.
Also make sure that your subscriber.php does not also have anything that will cause an output before header.
Footnotes:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
You should also use a conditional !empty() for your inputs.

Categories