$_SESSION messages not displaying - php

Please, I dont know what might be the problem that my $_SESSION['error'] does not display in my html file. I dont see what might be the problem.
I have error messages in session but none of them displays
in my html, i included my backend code that is php and session is started there
//HTML FILE
register.php
<? php
require_once 'php.php';
?>
<h4>Registration</h4>
<div><?php
if ( isset($_SESSION['error']) ) {
echo '<p style="color:red">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
echo '<p style="color:green">'.$_SESSION['success']."</p>\n";
unset($_SESSION['success']);
}
?>
</div>
<form action="about.php" method="post" enctype="multipart/form-data">
<p id="head"></p>
<!-- <div class="mb-3"> -->
<label for="surname" class="form-label">Surname</label>
<input class="form-control" type="text" id="surname" name="surname"
<label for="firstname" class="form-label">First name</label>
<input class="form-control" type="text" id="firstname" name="firstname" value="<?= htmlentities($firstname) ?>" required>
<label for="othername" class="form-label">Othername</label>
<input class="form-control" type="text" id="othername" name="othername" value="<?= htmlentities($othername) ?>"><br>
<button type="submit" id="submitform" name="signup-btn" class="btn btn-primary btn-inline-block btn-lg">Sign Up</button>
</form>
//PHP
<?php
session_start();
require_once "pdo.php";
if (isset($_POST['signup-btn'])) {
if (strlen($_POST['surname']) < 1) {
$_SESSION['error'] = "Please, add surname";
header("Location:register.php");
return;
}
if (strlen($_POST['firstname']) < 1) {
$_SESSION['error'] = "Please, add your first name";
header("Location:register.php");
return;
}

The session_start must be before any HTML is emitted. In the file, you have <h4> before session_start is executed.
Shift the session_start to the top of the file. If php.php has no HTML then it can be after the include, otherwise, put it before the include.

Related

Unable to submit form using jquery submit method

What i am trying to do is:
post a form when user is logged in.
but if he is not logged in then pop up login is shown to user.
and in that popup redirection URL is added to hidden field.
when popup opens and i login it redirect me to that form.
But when i try to submit form it not being submitted.
// submit button in form
$('#submitcompanyEnquiry').on('click',function(e){
e.preventDefault();
//get data attr to check if user is login
if($('#companyEnquiry').data('login')){
//companyEnquiry =>form id
//here i try to submit form
console.log('testing'); --->it is working
jQuery('#companyEnquiry').submit(); ---> //the problem is here this piece of code is executing
}else{
if($('#companyEnquiry').attr('action')!=''){
//here i added the current url to hidden field latter to used for redirection
$('#loginForm #redirectUrl').val($('#companyEnquiry').data('seotitle'));
}
//here the login popup is trigger.
jQuery("#login").trigger('click');
}
});
Things that I confirmed:
ensure that there is unique id with the
name provided.
console some value in the if block which was
running but the line of code i have mention.
PHP part is working fine i have removed the e.preventDefault();
it is works fine but doesn't achieve the require functionality.
HTML Code
<form action="<?=Route::url('default',array('controller'=>'contact','action'=>'user_contact'))?>" data-login="<?php echo $data; ?>" data-seotitle="<?=Route::url('company', array('controller'=>'listing','seotitle'=>$company_seotitle))?>" id="companyEnquiry" method="post">
<input type="hidden" name="company_to" value="<?php echo $id; ?>">
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" name="name" required aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" required value="<?php echo $auth->get_user()->company_name; ?>" name="name" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required name="company_from" aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required value="<?php echo $auth->get_user()->companyemail; ?>" name="company_from" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if ($auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" value="<?php echo $auth->get_user()->company_phone_1; ?>" aria-describedby="basic-addon1">
</div>
<?php } else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" aria-describedby="basic-addon1">
</div>
<?php } ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="subject" required name="subject" aria-describedby="basic-addon1">
</div>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="message" required name="message" aria-describedby="basic-addon1">
</div>
<input data-login="<?php echo $data; ?>" id="submitcompanyEnquiry" type="submit" name="submit" value="SEND" class="form-control blue-btn send-btn">
</form>
The problem can only exist in the way you are adding the login variable to the form with the id : companyEnquiry
Check if you have add it as correct parameter because jquerys data function will only read values which have a "data-" tag infront of it.
So your php code should look like this :
echo '<form id="companyEnquiry" ' . ($login ? 'data-login="1"' : '' . '>

PHP still not reading local file

After several attempts, I have no idea why the log in is not working. I'm trying to log the user in (user password and email stored on a .txt file, each line has count,useremail,password) and then redirect them to index2.php for only users to access. I'm not sure what I'm doing wrong.
log in form located in navbar in index.php
<form class="form" role="form" method="POST" action="login.php" accept-charset="UTF-8" id="login-nav">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" id="submit">Sign in</button>
</div>
</form>
login.php code
<?php session_start();?>
<?php
$email=$_POST['$email'];
$pass=$_POST['$password'];
echo $email;
$fileName = "Data/users.txt";
$target = fopen($fileName, "r");
//find user
$users=fgetcsv($target,200,",");
while(!feof($target)){
if($email==$users[1]&&$pass==$users[2]){
$_SESSION['email']=$users[1];
$_SESSION['id']=$users[0];
//if found redirect
$redirect= "index2.php";
header("Location:$redirect");
exit();
}
else{
unset($_SESSION['email']);
unset($_SESSION['id']);
echo "<script>";
echo "alert(Invalid login.);";
echo "location.href='index.php?error=login';";
echo "</script>";
}
}
fclose($target);
?>
You can't echo $email; and then do a header() redirect. You will have to enable output buffering:
ob_start();
echo $email;
// other code
header("Location:$redirect");
ob_end_flush();
And enable error reporting to see what is going wrong.

php coding Form submission to server using php

I have typed this coding by using php and mysql. In If statement condition Id part is not inserted into database. But other fields are inserted into database. I couldn't find out the error. Can anyone tell what is the error?
<?php
$conn=mysqli_connect("localhost","root","database","user") or die("unable to connect");
//echo "Connected Successfully";
?>
<h2>Form Submission to Server</h2>
<?php
if(isset($_POST["submit"]))
{// get user inputs
$error="";
$resultmessage="";
$id=$_POST["id"];
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$email=$_POST["email"];
$password=$_POST["password"];
//error messages
$missingfirstname="<p>Enter your First name</p>";
$missinglastname="<p>Enter your Last name</p>";
$missingemail="<p>Enter your Email id</p>";
$invalidmail="<p>enter your valid mail id</p>";
$missingpwd="<p>Enter your password</p>";
if(!$firstname)
{
$error.=$missingfirstname;
}
else
{
$firstname=filter_var($firstname,FILTER_SANITIZE_STRING);
}
if(!$lastname)
{
$error.=$missinglastname;
}
else
{
$lastname=filter_var($lastname,FILTER_SANITIZE_STRING);
}
if(!$email)
{
$error.=$missingemail;
}
else
{
$email=filter_var($email,FILTER_SANITIZE_EMAIL);
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$error.=$invalidmail;
}
}
if(!$password)
{
$error.=$missingpwd;
}
if($error)
{
$resultmessage='<div class="alert alert-danger">'.$error.'</div>';
echo $resultmessage;
}
else
{
//no errors,prepare variables for query
$tblname="attend4";
$firstname=mysqli_real_escape_string($conn,$firstname);
$lastname=mysqli_real_escape_string($conn,$lastname);
$email=mysqli_real_escape_string($conn,$email);
$password=mysqli_real_escape_string($conn,$password);
$password=md5($password);
//execute the query
if(!$id)
{
$sql="INSERT INTO attend4(firstname,lastname,email,password)VALUES('$firstname','$lastname','$email','$password')";
}
else
{
$sql="INSERT INTO attend4(id,firstname,lastname,email,password)VALUES('$id',$firstname','$lastname','$email','$password')";
} if(mysqli_query($conn,$sql))
{
$resultmessage="Data added successfully";
echo $resultmessage;
}
else
{
$resultmessage="unable to add";
echo $resultmessage;
}
}
}
?>
<form action="33.populateusigform.php" method="post">
<div class="form-group">
<label for="number">Number</label>
<input type="number" name="id" id="id" class="form-control" maxlength="4" placeholder="Enter your Number">
</div>
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" maxlength="30" class="form-control" placeholder="Enter your Firstname">
</div>
<div class="form-group">
<label for="lastname">Lastname</label>
<input type="text" name="lastname" id="lastname" maxlength="30" placeholder="Enter your lastname" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" maxlength="30" placeholder="Enter your Email Id"class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" maxlength="30" placeholder="Enter your Password" class="form-control">
</div>
<input type="submit" name="submit" class="btn btn-success center-block" value="Send data">
</form>
I suspect your issue is with the if($id) which may act strangely if given 0, triggering the sql query without the id term.
I would use if(empty($id)) instead it will be more reliable.
Too many problems...
1) you have kept your action "33.populateusigform.php" for form but are inserting data from same file....
2) have you kept your id to auto increment?
3) what is use to save id in your case, it will automatically be saved if you use as primary key in db.
Need to check your db schema for further assistance.

override include php if logged in

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

How do I get proper data while saving to mysql database?

I have a form made that appears to be connected to a mysql database. When I enter information into the form and submit it, it registers in the database as "0000-00-00" and the data is returned as "0000-00-00". None of the actual data is showing up. Any Ideas?
connection.php:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$db = 'sm_residents';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Create.php:
<?php
include ('connection.php');
$FirstName= $_POST['inputFirstName'];
$LastName= $_POST['inputLastName'];
$Address= $_POST['inputAddress'];
$Birthday= $_POST['inputBirthday'];
$FormerResidence= $_POST['inputFormerResidence'];
$Career= $_POST['inputCareer'];
$Education= $_POST['inputEducation'];
$SpecialInterests= $_POST['inputSpecialInterests'];
if ($_FILES["file"]["error"] > 0) {
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
}
}
Picture= $_FILES["file"]["name"];
mysql_query("INSERT INTO residents (`ID`,`FirstName`,`LastName`,`Address`,`Birthday`,`FormerResidence`,`Career`,`Education`,`SpecialInterests`,`Picture`)
VALUES(NULL,'$FirstName','$LastName','$Address','$Birthday','$FormerResidence','$Career','$Education','$SpecialInterests','$Picture')") or die(mysql_error());
?>
<script> window.location = "index.php"; </script>
Index.php:
<?php
include ('connection.php');
if(isset($_POST['submit'])) {
echo "Please Fill Out The Form";
//header ('Location: create.php');
} else {
//echo "User Has Been Added";
//header('Location: create.php');
}
?>
<h1>Add A Resident</h1>
<form action="create.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputFirstName">First Name</label>
<input type="text" class="form-control" id="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="inputLastName">Last Name</label>
<input type="text" class="form-control" id="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="inputBirthday">Birthday</label>
<input type="date" class="form-control" id="inputBirthday">
</div>
<div class="form-group">
<label for="inputFormerResidence">Former Residence</label>
<input type="text" class="form-control" id="inputFormerResidence" placeholder="Former Residence">
</div>
<div class="form-group">
<label for="inputCareer">Career</label>
<input type="text" class="form-control" id="inputCareer" placeholder="Career">
</div>
<div class="form-group">
<label for="inputEducation">Education</label>
<input type="text" class="form-control" id="inputEducation" placeholder="Education">
</div>
<div class="form-group">
<label for="inputSpecialInterests">Special Interests</label>
<input type="text" class="form-control" id="inputSpecialInterests" placeholder="Special Interests">
</div>
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" id="inputFile">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I'll make this an answer: (or a BIG 90% / 95% partial at best)
None of the actual data is showing up
There aren't even any named elements for the inputs, so nothing is going to go through, they're all IDs.
<input type="text" class="form-control" id="inputFirstName" placeholder="First Name">
which should read as
<input type="text" name="inputFirstName" class="form-control" id="inputFirstName" placeholder="First Name">
^^^^^^^^^^^^^^^^^^^^^
then do the same for the others by "naming" them as I did above.
PHP is looking for named elements, not IDs which cannot be relied upon.
This also doesn't have a name <input type="file" id="inputFile">
change to: <input type="file" name="file" id="inputFile">
as per $_FILES["file"]
Use error reporting
while placing the following underneath your opening <?php tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
which will throw many Undefined index warnings with your present code.
Also as pointed out by Barmar in his comment, you're missing a $ for Picture= $_FILES["file"]["name"]; unless that was a typo, it should read as:
$Picture= $_FILES["file"]["name"];
In regards to it registers in the database as "0000-00-00" make sure your column is of the correct type to accomodate a DATE.
Try formatting first your Birthdate:
$Birthday = date("Y-m-d", strtotime($_POST['inputBirthday']));

Categories