I have a problem with the bootstrap modal, it doesn't insert data in MySQL even though i have a similar modal in the same page that updates the data.. The data is represented in tables with delete and update buttons for each row... Which both work well in the same page.. I am not using Ajax and i was hoping this would work without using any other technologies.. it's kinda odd as the update query uses the same technique and still works regardless
<?php
require_once "../db.php";
session_start();
if(!isset($_SESSION['admin'])){
header('location:login.php');
}
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
?>
<!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.0">
<title>Document</title>
<link rel="stylesheet" href="../css/all.css">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<script src="../js/jquery-3.5.1.min.js"></script>
<script srC="../js/bootstrap.min.js"></script>
<style>
.nav-link{
font-size:18px;
font-weight:bold;
}
</style>
</head>
<body>
<?php
if(isset($_POST['confirmadd'])){
$code = $_POST['voyage'];
$newrow0 = $_POST['depart'];
$newrow1 = $_POST['villedep'];
$newrow2 = $_POST['arrivee'];
$newrow3 = $_POST['villearr'];
$newrow4 = $_POST['price'];
$addTrip = mysqli_query($sql,"INSERT INTO voyage VALUES('$code','$newrow0','$newrow1',$newrow2','$newrow3','$newrow4');");
if($addTrip){
redirect('dashboard.php');
}else{
echo "error";
}
}
?>
<div class="container-fluid bg-info sticky-top">
<div class="row d-flex align-items-center">
<div class="col-6 offset-1">
<h2 class="display-4 text-white">AnouBus</h2>
</div>
<div class="col-4">
<div class="navbar navbar-expand-lg navbar-light navbar-fixed-top">
<ul class="navbar navbar-nav">
<li class="nav-item"><a class="nav-link" href="logout.php">Logout</a></li>
</ul>
</div>
</div>
</div>
</div>
<h1 class="text-center">Your Trips</h1>
<button class="btn btn-primary mx-2" style="float:right;" data-toggle="modal" data-target="#addmodal">Add Trip</button>
<div class="modal fade" id="addmodal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header"><h2 class="text-center">Update Trip</h2></div>
<div class="modal-body">
<form method="post">
<div class="form-row">
<div class="form-group col-12">
<input type="text" name="voyage" class="form-control" placeholder="Trip Code">
</div>
<div class="form-group col-12">
<input type="text" name="depart" class="form-control" placeholder="Departure Time" ?>
</div>
<div class="form-group col-12">
<input type="text" name="villedep" class="form-control" placeholder="Departure City">
</div>
<div class="form-group col-12">
<input type="text" name="arrivee" class="form-control" placeholder="Arrival Time">
</div>
<div class="form-group col-12">
<input type="text" name="villearr" class="form-control" placeholder="Arrival City">
</div>
<div class="form-group col-12">
<input type="text" name="price" class="form-control" placeholder="Price">
</div>
<div class="form-group col-12">
<input type="submit" class="form-control btn btn-success" name="confirmadd" value="confirm">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Related
I just uploaded a website on live before that I test on PHP local host and it's perfectly fine no errors at all. But when I upload it live the insert statements are not working. But other SQL is perfectly fine like retrieve select statements all others are perfectly fine. I'm already connected to the database.
<?php
session_start();
error_reporting(0);
include('includes/dbconnection.php');
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$services=$_POST['services'];
$adate=$_POST['adate'];
$atime=$_POST['atime'];
$phone=$_POST['phone'];
$aptnumber = mt_rand(100000000, 999999999);
$sqlinsert = "INSERT INTO `tblappointment` (`AptNumber`, `Name`, `Email`, `PhoneNumber`, `AptDate`, `AptTime` ,`Services`) VALUES ('$aptnumber','$name','$email','$phone','$adate','$atime','$services')";
if (!mysqli_query($con, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 new record added to the database";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>teethenjoy Dental Clinic Official Website</title>
<link href="https://fonts.googleapis.com/css?family=Work+Sans:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Playfair+Display:400,400i,700,700i,900,900i" rel="stylesheet">
<link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/aos.css">
<link rel="stylesheet" href="css/ionicons.min.css">
<link rel="stylesheet" href="css/bootstrap-datepicker.css">
<link rel="stylesheet" href="css/jquery.timepicker.css">
<link rel="stylesheet" href="css/flaticon.css">
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php include_once('includes/header.php');?>
<!-- END nav -->
<section id="home-section" class="hero" style="background-image: url(images/8.jpg);" data-stellar-background-ratio="0.5">
<div class="home-slider owl-carousel">
<div class="slider-item js-fullheight">
<div class="overlay"></div>
<div class="container-fluid p-0">
<div class="row d-md-flex no-gutters slider-text align-items-end justify-content-end" data-scrollax-parent="true">
<!--<img class="one-third align-self-end order-md-last img-fluid" src="images/1.png" alt=""> -->
<div class="one-forth d-flex align-items-center ftco-animate" data-scrollax=" properties: { translateY: '70%' }">
<div class="text mt-5">
<span class="subheading">teethenjoy Dental Clinic</span>
<h1 class="mb-4">Get Pretty Smile</h1>
<h2 class="mb-4">A SMILE is the PRETTIEST THING YOU'LL ever wear</h2>
</div>
</div>
</div>
</div>
</div>
<div class="slider-item js-fullheight">
<div class="overlay"></div>
<div class="container-fluid p-0">
<div class="row d-flex no-gutters slider-text align-items-center justify-content-end" data-scrollax-parent="true">
<!-- <img class="one-third align-self-end order-md-last img-fluid" src="images/2.png" alt=""> -->
<div class="one-forth d-flex align-items-center ftco-animate" data-scrollax=" properties: { translateY: '70%' }">
<div class="text mt-5">
<span class="subheading">teethenjoy Dental Clinic</span>
<h1 class="mb-4">Get a Confidence </h1>
<h2 class="mb-4">A warm smile is the universal language of kindness</h2>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<br>
<section class="ftco-section ftco-no-pt ftco-booking">
<div class="container-fluid px-0">
<div class="row no-gutters d-md-flex justify-content-end">
<div class="one-forth d-flex align-items-end">
<div class="text">
<div class="overlay"></div>
<div class="appointment-wrap">
<span class="subheading">Reservation</span>
<h3 class="mb-2">Make an Appointment</h3>
<form action="#" method="post" class="appointment-form">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Name" name="name" required="true">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="email" class="form-control" id="appointment_email" placeholder="Email" name="email" required="true">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="select-wrap">
<div class="icon"><span class="ion-ios-arrow-down"></span></div>
<select name="services" id="services" required="true" class="form-control">
<option value="">Select Services</option>
<?php $query=mysqli_query($con,"select * from tblservices");
while($row=mysqli_fetch_array($query))
{
?>
<option value="<?php echo $row['ServiceName'];?>"><?php echo $row['ServiceName'];?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control appointment_date" placeholder="Date" name="adate" id='adate' required="true">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control appointment_time" placeholder="Time" name="atime" id='atime' required="true">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" required="true" maxlength="10" pattern="[0-9]+">
</div>
</div>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Book an Appointment" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
<div class="one-third">
<div class="img" style="background-image: url(images/9.jpg);">
</div>
</div>
</div>
</div>
</section>
<br>
<?php include_once('includes/footer.php');?>
<!-- loader -->
<div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px"><circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/><circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00"/></svg></div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery-migrate-3.0.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/jquery.waypoints.min.js"></script>
<script src="js/jquery.stellar.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/jquery.magnific-popup.min.js"></script>
<script src="js/aos.js"></script>
<script src="js/jquery.animateNumber.min.js"></script>
<script src="js/bootstrap-datepicker.js"></script>
<script src="js/jquery.timepicker.min.js"></script>
<script src="js/scrollax.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVWaKrjvy3MaE7SQ74_uJiULgl1JY0H2s&sensor=false"></script>
<script src="js/google-map.js"></script>
<script src="js/main.js"></script>
</body>
</html>
I have a page named as add_administrator.php in this php i am checking if the status value is given using the GET method and if so that display the header with the value in status Now i am getting the value form a page add_administrator_code.php But the problem is that i am getting the header more then once i.e. Three times with the same message What should i do?
here is the add_administrator.php
<?php
session_start();
session_regenerate_id(true);//regenerating session id on every reload or refresh so to avoid the session hijack
if (isset($_GET['status'])) {
$sta = $_GET['status'];
echo "<script type='text/javascript'>alert('$sta');</script>";
}
if($_SESSION['alogin'])
{
}
else
{
header("location:../index.php");
}
?>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Smart Ambulance</title>
<link rel="stylesheet" href="assets/css/normalize.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/themify-icons.css">
<link rel="stylesheet" href="assets/css/flag-icon.min.css">
<link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
<!-- <link rel="stylesheet" href="assets/css/bootstrap-select.less"> -->
<link rel="stylesheet" href="assets/scss/style.css">
<link href="assets/css/lib/vector-map/jqvmap.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
<!-- Fav and touch icons -->
<?php include 'logo.php';?>
</head>
<body>
<!-- Left Panel -->
<aside id="left-panel" class="left-panel"style="background-color:#004466">
<nav class="navbar navbar-expand-sm navbar-default">
<?php include 'left_menu.php';?>
</nav>
</aside><!-- /#left-panel -->
<!-- Left Panel -->
<!-- Right Panel -->
<div id="right-panel" class="right-panel">
<!-- Header-->
<header id="header" class="header">
<?php include 'header_menu.php';?>
</header><!-- /header -->
<!-- Header-->
<div class="breadcrumbs">
<div class="col-sm-4">
<div class="page-header float-left">
<div class="page-title">
<h1>Dashboard</h1>
</div>
</div>
</div>
<div class="col-sm-8">
<div class="page-header float-right">
<div class="page-title">
<ol class="breadcrumb text-right">
<li class="active">Dashboard / User Details / Adminsitrator / Add</li>
</ol>
</div>
</div>
</div>
</div>
<div class="content mt-3">
<!--********************************************** Content Start **********************************************-->
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<strong>Add Adminsitrator</strong>
</div>
<div class="card-body card-block">
<form action="add_administrator_code.php" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="row form-group">
<!--<div class="col col-md-3"><label class=" form-control-label">Static</label></div> -->
<div class="col-12 col-md-9">
<!-- <p class="form-control-static">Username</p> -->
</div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="text-input" class=" form-control-label">Name</label></div>
<div class="col-12 col-md-9"><input type="text" id="adm_name" name="adm_name" placeholder="Name" class="form-control"><small class="form-text text-muted"></small></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="email-input" class=" form-control-label">Contact Number</label></div>
<div class="col-12 col-md-9"><input type="text" id="adm_mob" name="adm_mob" placeholder="Enter Contact Number" class="form-control"><small class="help-block form-text"></small></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="email-input" class=" form-control-label">Email Id</label></div>
<div class="col-12 col-md-9"><input type="email" id="adm_mail_id" name="adm_mail_id" placeholder="Enter Email" class="form-control"><small class="help-block form-text"></small></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="password-input" class=" form-control-label">Password</label></div>
<div class="col-12 col-md-9"><input type="password" id="adm_pass" name="adm_pass" placeholder="Password" class="form-control"><small class="help-block form-text">Press the Generate Button to generate password</small></div>
</div>
<div class="row form-group">
<div align =center class="col col-md-1"><input type="checkbox" onclick="togglepassword()" ></div>
<label for="text-input" class=" form-control-label">Show Password</label>
</div>
<div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
<button type="reset" class="btn btn-danger btn-sm">
<i class="fa fa-ban"></i> Reset</i>
</button>
<button type="button" onclick="generate()" class="btn btn-success btn-sm">Generate</button>
</div>
</div>
<!--********************************************** Content End **********************************************-->
</div>
</div><!-- /#right-panel -->
<!-- Right Panel -->
<script src="assets/js/vendor/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="assets/js/plugins.js"></script>
<script src="assets/js/main.js"></script>
<script src="assets/js/lib/chart-js/Chart.bundle.js"></script>
<script src="assets/js/dashboard.js"></script>
<script src="assets/js/widgets.js"></script>
<script src="assets/js/lib/vector-map/jquery.vmap.js"></script>
<script src="assets/js/lib/vector-map/jquery.vmap.min.js"></script>
<script src="assets/js/lib/vector-map/jquery.vmap.sampledata.js"></script>
<script src="assets/js/lib/vector-map/country/jquery.vmap.world.js"></script>
<script>
( function ( $ ) {
"use strict";
jQuery( '#vmap' ).vectorMap( {
map: 'world_en',
backgroundColor: null,
color: '#ffffff',
hoverOpacity: 0.7,
selectedColor: '#1de9b6',
enableZoom: true,
showTooltip: true,
values: sample_data,
scaleColors: [ '#1de9b6', '#03a9f5' ],
normalizeFunction: 'polynomial'
} );
} )( jQuery );
</script>
<script>
function togglepassword() {
var x = document.getElementById("adm_pass");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
</script>
<script>
function generate(){
//set password length/complexity
let complexity = 8;
//possible password values
let values = "ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz1234567890!##$%^&*()_+";
let password = "";
//create for loop to choose password characters
for(var i = 0; i <= complexity; i++){
password = password + values.charAt(Math.floor(Math.random() * Math.floor(values.length - 1)));
}
//add password to textbox/display area
document.getElementById("adm_pass").value = password;
}
</script>
</body>
</html>
here is the add_administrator_code.php
<?php include '../database.php';
// create a variable
$adm_name=$_POST['adm_name'];
$adm_mob=$_POST['adm_mob'];
$adm_mail_id=$_POST['adm_mail_id'];
$adm_pass=$_POST['adm_pass'];
mysqli_query($con,"INSERT INTO admin_details (adm_name,adm_mob,adm_mail_id,adm_pass)
VALUES ('$adm_name','$adm_mob','$adm_mail_id','$adm_pass')");
if(mysqli_affected_rows($con) > 0){
$status = "New Admin added Sucessfully";
header("location: add_administrator.php?status=".$status);
} else {
$status = "Error in adding New Admin";
header("location: add_administrator.php?status=".$status);
}
?>
You need to stop the script after you redirect to make sure nothing else runs:
{
header("location: ...");
exit;
}
Also, your code is vulnerable to both XSS and SQL injection. Read about escaping HTML and prepared statements.
for preventing loop the session start: instead of
session_start();
session_regenerate_id(TRUE);
change to this:
if (session_status() == PHP_SESSION_NONE) {
session_start();
session_regenerate_id(TRUE);
}
update me with the result,
Note: dont forget to normalize the $_GET method for the security
improvement
I'm currently having some issues with creating a webpage that's only accessible once a user has logged in.
I've looked through various threads here, but to no avail. Any help with this would be greatly appreciated.
Here's my code:
login.php
<?php
Include('connect.php');
if (isset($_REQUEST['Submit']))
{
if($_REQUEST['user_id']=="" || $_REQUEST['password']=="")
{
echo " Field must be filled";
}
else
{
$sql1= "select * from student where email= '".$_REQUEST['user_id']."' && password ='".$_REQUEST['password']."'";
$result=mysql_query($sql1)
or exit("Sql Error".mysql_error());
$num_rows=mysql_num_rows($result);
if($num_rows>0)
{
session_start($_SESSION['Login']);
Echo "You have logged in successfully";
header("Location: statistics.html");
}
else
{
echo "Wrong username or password.";
}
}
}
?>
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>PHP Login Form</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="container">
<div class="row" style="margin-top:20px">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<form name="form_login" method="post" action="login.php" role="form">
<fieldset>
<h2>Please Sign In</h2>
<hr class="colorgraph">
<div class="form-group">
<input name="user_id" type="text" id="user_id" class="form-control input-lg" placeholder="Email Address">
</div>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password">
</div>
<span class="button-checkbox">
<button type="button" class="btn" data-color="info">Remember Me</button><!-- Additional Option -->
<input type="checkbox" name="remember_me" id="remember_me" checked="checked" class="hidden">
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" name="Submit" value="Login" class="btn btn-lg btn-success btn-block">
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
And statistics.html (Page that should only be accessible when logged in)
<?php
include ("login.php")
session_start();
if(!isset($_SESSION['Login']))
{
header("Location:login.php");
die();
}
?>
<!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">
<title>Personal Website</title>
<link rel="stylesheet" href="../../CSS/stylesheetmain.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
</head>
<body>
<!--Navigation Bar-->
<div class="row">
<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
<nav class="navbar navbar-background-color">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><!--Reference: Bootstrap, 2015. getbootstrap.com. [Online] Available at: http://getbootstrap.com/ [Accessed 01 April 2015]-->
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../index.html"></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home</li><!--Edits made: Removed active button -->
<li>About Me</li>
<li>Blog</li>
<li>Contact Me</li>
<li>Login</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
</div>
<div class="row">
<div class="navbarbottom column col-md-8 col-md-offset-2 col-xs-12 col-s-12"></div>
</div>
<!--Page Title-->
<div class="row">
<div class="title mediumbluetext col-md-8 col-md-offset-2 col-xs-0 col-s-0">
<center><h1>Statistics</h1></center>
</div>
</div>
<!--Main Body-->
<div class="row">
<!--Left Column Spacer-->
<div class="maintextleftbackground column col-md-2 col-xs-0 col-s-0">
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52f8f8c0164b330f" async="async"></script>
</div>
<!--Middle Column-->
<div class="maintext80 column col-md-8 col-xs-12 col-s-8 col-s-offset-2"><br>
<div id="main-chart-container"></div>
<div id="breakdown-chart-container"></div>
<div id="embed-api-auth-container"></div>
<div id="view-selector-container"></div>
</div>
<!--Right Column Spacer-->
<div class="maintextrightbackground column col-md-2 col-xs-0 col-s-0">
</div>
</div>
<!--Footer Bar-->
<div class="row">
<div class="darkgrey column col-md-8 col-md-offset-2 col-xs-12 col-s-12">
<nav class="navbar-background-color">
<div class="container-fluid">
<p class="navbar-text navbar-right">SiteMap</p>
</div>
</nav>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>
(function(w,d,s,g,js,fs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>
</body>
</html>
No matter what I try, I can't seem to get the code to work. The files are in the same directory as well.
Any help with this would be greatly appreciated.
A few things wrong here but you're on the right track.
You are VERY susceptible to SQL injection. Please read about it.
Your form uses POST so your PHP should use $_POST and not $_REQUEST
You cannot set a session variable by passing it through to session_start. You need to set the variable like so: $_SESSION['isLoggedIn'] = true.
You are echoing right before you issue a header command. You cannot output anything before redirecting.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
I'm trying to create a login script, which generates a session after the user signed in, now the user would be redirected to a new url, sadly I receive the following error:
[Fri Sep 23 20:09:17.125738 2016] [fcgid:warn] [pid 27802] [client 91.97.73.183:12550] mod_fcgid: stderr: PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/pr0b.com/httpdocs/index.php:109) in /var/www/vhosts/pr0b.com/httpdocs/handling/classes/user_login_class.php on line 74, referer: http://pr0b.com/
My code looks like the following:
Index.php
<?php
require_once('handling/classes/database_class.php');
require_once('handling/classes/site_config_class.php');
require_once('handling/classes/main_class.php');
$site_config = new site_config_class();
$site_informations = new main_class();
$site_informations_response = $site_informations->class_handler();
?>
<!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">
<title><?php echo $site_config->site_title_home; ?></title>
<!-- Global stylesheets -->
<link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
<link href="assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
<link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="assets/css/core.css" rel="stylesheet" type="text/css">
<link href="assets/css/components.css" rel="stylesheet" type="text/css">
<link href="assets/css/colors.css" rel="stylesheet" type="text/css">
<link href="assets/css/stylesheet.css" rel="stylesheet" type="text/css">
<!-- Core JS files -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/core/libraries/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/plugins/ui/nicescroll.min.js"></script>
<script type="text/javascript" src="assets/js/plugins/ui/drilldown.js"></script>
<script type="text/javascript" src="assets/js/pages/form_select2.js"></script>
<script type="text/javascript" src="assets/js/core/app.js"></script>
<script type="text/javascript" src="assets/js/plugins/forms/selects/select2.min.js"></script>
<script type="text/javascript" src="../assets/js/plugins/notifications/noty.min.js"></script>
</head>
<body class="navbar-top-md-md login-container">
<div class="navbar-fixed-top">
<div class="navbar navbar-inverse">
<div class="navbar-header">
<a class="navbar-brand" href="index.html"><div class="logo"></div></a>
<ul class="nav navbar-nav pull-right visible-xs-block">
<li><a data-toggle="collapse" data-target="#navbar-mobile"><i class="icon-tree5"></i></a></li>
</ul>
</div>
<div class="navbar-collapse collapse" id="navbar-mobile">
<ul class="nav navbar-nav navbar-right">
<li><a data-toggle="modal" data-target="#modal-registration"><i class="icon-users4 position-left"></i> Register Account</a></li>
<li><a data-toggle="modal" data-target="#modal-login"><i class="icon-user-lock position-left"></i> Log In</a></li>
<li><a data-toggle="modal" data-target="#modal-recover"><i class="icon-reply position-left"></i> Resend Credentials</a></li>
</ul>
</div>
</div>
<!-- /main navbar -->
<!-- Second navbar -->
<div class="navbar navbar-default" id="navbar-second">
<ul class="nav navbar-nav no-border visible-xs-block">
<li><a class="text-center collapsed" data-toggle="collapse" data-target="#navbar-second-toggle"><i class="icon-menu7"></i></a></li>
</ul>
<div class="navbar-collapse collapse" id="navbar-second-toggle">
<ul class="nav navbar-nav">
<li class="active"><i class="icon-home position-left"></i> Home</li>
<li><i class="icon-heart6 position-left"></i> Community</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><i class="icon-facebook2"></i></li>
<li><i class="icon-twitter"></i></li>
<li><i class="icon-youtube"></i></li>
</ul>
</div>
</div>
<!-- /second navbar -->
</div>
<!-- /fixed navbars wrapper -->
<!-- Page container -->
<div class="page-container">
<div class="page-content">
<div class="content-wrapper">
<div class="row">
<div class="col-lg-3">
<div class="panel bg-slate-400">
<div class="panel-body">
<div class="heading-elements">
<span class="heading-text badge bg-slate-300">Registered users</span>
</div>
<h3 class="no-margin"><?php echo $site_informations_response['registered_users']; ?></h3>
Registered users
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel bg-slate-400">
<div class="panel-body">
<div class="heading-elements">
<span class="heading-text badge bg-slate-300">Online users</span>
</div>
<h3 class="no-margin"><?php echo $site_informations_response['online_users']; ?></h3>
Online users
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel bg-slate-400">
<div class="panel-body">
<div class="heading-elements">
<span class="heading-text badge bg-slate-300">Male users</span>
</div>
<h3 class="no-margin"><?php echo $site_informations_response['male_users']; ?></h3>
Male users
</div>
</div>
</div>
<div class="col-lg-3">
<div class="panel bg-slate-400">
<div class="panel-body">
<div class="heading-elements">
<span class="heading-text badge bg-slate-300">Female users</span>
</div>
<h3 class="no-margin"><?php echo $site_informations_response['female_users']; ?></h3>
Female users
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
Content
</div>
</div>
<!-- Login form -->
<div id="modal-login" class="modal fade">
<div class="modal-dialog">
<div class="modal-content login-form">
<!-- Form -->
<form class="modal-body" method="post">
<div class="text-center">
<div class="icon-object border-slate-300 text-slate-300"><i class="icon-reading"></i></div>
<h5 class="content-group">Login to your account <small class="display-block">Your credentials</small></h5>
</div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Username" name="username">
<div class="form-control-feedback">
<i class="icon-user text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Password" name="password">
<div class="form-control-feedback">
<i class="icon-lock2 text-muted"></i>
</div>
</div>
<input type="hidden" name="action" value="login">
<div class="form-group">
<button type="submit" class="btn bg-slate-600 btn-block">Login</button>
<button type="button" class="btn btn-default btn-block" data-dismiss="modal">Cancel</button>
</div>
<span class="help-block text-center no-margin">By continuing, you're confirming that you've read our Terms & Conditions and Cookie Policy</span>
</form>
<!-- /form -->
</div>
</div>
</div>
<!-- /login form -->
<!-- Registration form -->
<div id="modal-registration" class="modal fade">
<div class="modal-dialog">
<div class="modal-content login-form">
<!-- Form -->
<form class="modal-body" method="post">
<div class="text-center">
<div class="icon-object border-slate-300 text-slate-300"><i class="icon-plus3"></i></div>
<h5 class="content-group">Create account <small class="display-block">All fields are required</small></h5>
</div>
<div class="content-divider text-muted form-group"><span>Your credentials</span></div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Username" name="username">
<div class="form-control-feedback">
<i class="icon-user-check text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<input id="password" type="password" class="form-control" placeholder="Password" name="password">
<div class="form-control-feedback">
<i class="icon-user-lock text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<input id="password_confirm" type="password" class="form-control" placeholder="Repeat password" name="password_confirm">
<div class="form-control-feedback">
<i class="icon-user-lock text-muted"></i>
</div>
</div>
<div class="form-group">
<span id="password_status" class="help-block text-center no-margin"></span>
</div>
<div class="content-divider text-muted form-group"><span>Your privacy</span></div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Email" name="email">
<div class="form-control-feedback">
<i class="icon-mention text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Reset pin code" name="pin_code">
<div class="form-control-feedback">
<i class="icon-sync text-muted"></i>
</div>
</div>
<div class="form-group">
<select data-placeholder="Select a gender..." class="select" name="gender">
<option></option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<div class="content-divider text-muted form-group"><span>Additions</span></div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" class="styled" name="terms">Accept terms of service
</label>
</div>
</div>
<input type="hidden" name="action" value="register">
<div class="form-group">
<button id="registerButton" type="submit" class="btn bg-slate-600 btn-block" disabled>Register account</button>
<button type="button" class="btn btn-default btn-block" data-dismiss="modal">Cancel</button>
</div>
<span class="help-block text-center no-margin">By continuing, you're confirming that you've read our Terms & Conditions and Cookie Policy</span>
</form>
<!-- /form -->
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#password_confirm").keyup(validate);
});
$('#terms').change(function(){
$("#registerButton").prop('disabled', !$(this).is(':checked'));
});
function validate() {
var password1 = $("#password").val();
var password2 = $("#password_confirm").val();
if(password1 == password2) {
$("#password_status").text("Passwords match!");
}
else {
$("#password_status").text("Passwords do not match!");
}
}
</script>
<!-- /registration form -->
<!-- Password recovery form -->
<div id="modal-recover" class="modal fade">
<div class="modal-dialog">
<div class="modal-content login-form">
<!-- Form -->
<form class="modal-body" action="index.html">
<div class="text-center">
<div class="icon-object border-slate-300 text-slate-300"><i class="icon-spinner11"></i></div>
<h5 class="content-group">Password recovery <small class="display-block">We'll send you instructions in email</small></h5>
</div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Username">
<div class="form-control-feedback">
<i class="icon-user-check text-muted"></i>
</div>
</div>
<div class="form-group has-feedback has-feedback-left">
<input type="text" class="form-control" placeholder="Reset pin code">
<div class="form-control-feedback">
<i class="icon-sync text-muted"></i>
</div>
</div>
<input type="hidden" name="action" value="recover">
<button type="submit" class="btn bg-slate-600 btn-block">Reset password</button>
<button type="button" class="btn btn-default btn-block" data-dismiss="modal">Cancel</button>
</form>
<!-- /form -->
</div>
</div>
</div>
<!-- /password recovery form -->
<?php
if(!empty($_POST)){
$action = $_POST['action'];
switch ($action) {
case 'login':
require_once('handling/classes/user_login_class.php');
$user_login = new user_login_class($_POST['username'], $_POST['password']);
$user_login_response = $user_login->class_handler();
echo $user_login_response;
break;
case 'register':
require_once('handling/classes/register_class.php');
$register_account = new register_class($_POST['username'], $_POST['password'], $_POST['password_confirm'], $_POST['email'], $_POST['pin_code'], $_POST['gender']);
$register_account_response = $register_account->class_handler();
echo $register_account_response;
break;
case 'recover':
#code
break;
}
}
?>
</div>
</div>
</div>
<!-- /Page container -->
<!-- Footer -->
<div class="footer text-muted text-center">
<?php echo $site_config->site_footer; ?>
</div>
<!-- /footer -->
</body>
</html>
user_login_class
<?php
class user_login_class extends database_class {
// Class constructor
function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
$this->salt = 'zWorlDs4lt';
$this->hashed_password = md5($this->salt.$this->password);
}
// Function to check the user credentials
function check_user_credentials() {
$this->connect();
$result = $this->execute_query("SELECT password FROM Users WHERE username = '" . $this->username . "'");
while($row = mysqli_fetch_assoc($result)) {
$response = $row['password'];
}
if($response !== $this->hashed_password) {
return false;
}else {
return true;
}
}
// Function to check the account status
function get_account_status() {
$this->connect();
$result = $this->execute_query("SELECT status FROM Users WHERE username = '" . $this->username . "'");
while($row = mysqli_fetch_assoc($result)) {
$response = $row['status'];
}
return $response;
}
// Function to update user informations
function update_user_variables() {
$this->connect();
$result = $this->execute_query("UPDATE Users SET ip = '" . $_SERVER['REMOTE_ADDR'] . "', online = '1' WHERE username = '" . $this->username . "'");
if(!$result) {
return false;
}else {
return true;
}
}
// Function to execute the class
function class_handler() {
if(!$this->username) {
return 'Error: Username is required. Please enter a valid username.';
}else if(!$this->password) {
return 'Error: Password is required. Please enter a valid password.';
}else {
$check_account_status = $this->get_account_status();
if($check_account_status == 1) {
return 'Error: This account was banned. Please contact our support.';
}else {
$check_user_credentials_resonse = $this->check_user_credentials();
if($check_user_credentials_resonse == false) {
return 'Error: Wrong credentials. Please try again.';
}else if($check_user_credentials_resonse == true) {
$update_user = $this->update_user_variables();
if($update_user == false) {
return 'Error: Wrong credentials. Please try again.';
}else if($update_user == true) {
session_start();
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $this->username;
header('Location: /zworld');
exit();
}
}
}
}
}
}
?>
But yea, it would not redirect the user, does anyone know why? I would appreciate any kind of help, cause I have no clue where to search.
at the very top of your script, put ob_start()
<?php
ob_start();
require_once('handling/classes/database_class.php');
require_once('handling/classes/site_config_class.php');
require_once('handling/classes/main_class.php');
$site_config = new site_config_class();
$site_informations = new main_class();
$site_informations_response = $site_informations->class_handler();
...
?>
I just started learning php and my first project was to make a working contact form. I have typed up what I could, but for some reason there appears to be some sort of errors that php is not telling me. I have looked for solutions to the issue on another web forum like phpacademy, and to no avail. I finally came to try out stack overflow, since I have heard great things about this forum. Any help is appreciated, and thanks in advance.
<!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">
<title>Contact</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/styles.css"/>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't websites if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
if ($_POST['submit']){
if(!$_POST['firstname']){
$error='<br /> Please Enter your first name';
}
if(!$_POST['lastname']){
$error='<br /> Please Enter your last name';
}
if(!$_POST['email']){
$error='<br /> Please Enter your email address';
}
if(!$_POST['phone']){
$error='<br /> Please Enter your phone number';
}
if(!$_POST['message']){
$error='<br /> Please Enter your message you would like to send';
}
if($_POST['email']!="" and !filter_var($_POST['email'],
FILTER_VALIDATE_EMAIL)){
$error.='<br /> Please enter a valid email address';
}
if($error){
$result='<div class="alert alert-danger"><strong>One or more errors has occurred</strong>'.$error.'</div>';
}
else {
if (mail("casey#caseywoelfle.com", "Potential Client!", "FirstName: " .
$_POST['firstname'] . "
LastName ".$_POST['lastname'] ."
Email: " .$_POST['email'] . "
Phone: ".$_POST['phone'] . "
Comment: ".$_POST['comment'])) {
$result = '<div class="alert alert-success"><strong>Thank you!</strong>I\'ll be in touch within 2 business days.</div>';
} else {
$result = '<div class="alert alert-danger"> Sorry, there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<nav class="navbar">
<div class="container-full">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand honey-sb" href="#">CW</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a class="nLink" href="index.html">Home</a></li>
<li><a class="nLink" href="about.html">About</a></li>
<li><a class="nLink" href="portfolio.html">Portfolio</a></li>
<li><a class="nLink" href="contact.php">Contact</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row contact-sec">
<div class="col-md-12">
<form class="form-horizontal" method="post">
<div class="well">
<h1 class="text-center header">Contact Me</h1>
<h3 class="text-center">
Do you have a project you would like me to take a look at or maybe you have an idea?<br/>Either
way don’t hesitate to send me an email using the contact form or give me a call.
</h3>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
<div class="col-md-6">
<input id="InputFirstName" name="firstname" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-user"></i></span>
<div class="col-md-6">
<input id="InputLastName" name="lastname" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-envelope"></i></span>
<div class="col-md-6">
<input id="InputEmail" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-earphone"></i></span>
<div class="col-md-6">
<input id="InputPhone" name="phone" type="text" placeholder="Phone" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="glyphicon glyphicon-pencil"></i></span>
<div class="col-md-6">
<textarea class="form-control" id="InputMessage" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="well">
<div class="form-group">
<div class="col-sm-4 col-sm-offset-4 text-center">
<button type="submit" id="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<footer>
<div class="container-fluid">
<div class="row" id="f-nav">
<div class="col-xs-6 col-sm-3 text-center" id="f-contact">
<h2 class="footerbolder">Contact</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-about">
<h2 class="footerbolder">About Us</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-services">
<h2 class="footerbolder">Services</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
<div class="col-xs-6 col-sm-3 text-center" id="f-follow">
<h2 class="footerbolder">Follow Me</h2>
<img src="images/ph.png" class="img-responsive" alt=""/>
</div>
</div>
<div class="row">
<div class="hidden-xs col-sm-12" id="pp-tos">
<div class="center-block text-center">
<a class="" href="#">Terms of Service</a>
<a>|</a>
<a class="" href="#">Privacy Policy</a>
</div>
</div>
</div>
</div>
</footer>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/Fittext/jquery.fittext.js"></script>
</body>
</html>
#B-and-P is right, if all your error checking validates, then your error variable is undefined by the time you reach your
if($error)
statement. To fix this, simply declare your error variable at the top to be null like
$error = NULL;
that way when you check for it, it is defined and if there is no errors, your script will send your mail.
There is no action attribute in your form tag. Example:
<form class="form-horizontal" method="post" action="send_form_email.php">
cut the PHP script from your code and put it in a separate file. Save as send_form_email.php
http://askguyson.com/simple-form-to-email-php-contact-form/