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.
Related
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.
After submit in signup page my signUp page redirects to info.php where I want to collect additional info of user using email id he gives on signup page but when I tried to get the email id of user through sessions, session return empty value.
THIS IS MY SIGNUP CODE
<?php
session_start();
if(isset($_POST['submit'])){
$name= $_POST['_user'];
$email = $_POST['_email'];
$pass = $_POST['_password'];
//Insert Data
$sql = "INSERT INTO signup(name,email,password)
VALUES('$name','$email','$pass')";
//Data Validation
if(mysqli_query($conn,$sql)){
echo "<script>alert('SignUp Successfull')</script>";
$_SESSION['user_email'] = $email;
header('Location: info.php');
}
else{
echo "<script>window.alert('You are already a user.')</script>";
}
}
mysqli_close($conn);
?>
AND THIS MY INFO.PHP CODE
<?php
session_start();
if(isset($_POST['_submit'])){
if(empty($_POST['_address']) || empty($_POST['_country']) || empty($_POST['_number']) || empty($_POST['_cnic']) || empty($_POST['_passport'])){
echo "<script>window.alert('All fields are required')</script>";
}
else{
$address = $_POST['_address'];
$country = $_POST['_country'];
$number = $_POST['_number'];
$cnic = $_POST['_cnic'];
$passport = $_POST['_passport'];
$email=$_SESSION['user_email'];
$query = "INSERT INTO info(email,address,country,mobile,cnic,passport)
VALUES('$email','$address','$country','$number','$cnic','$passport')";
if(mysqli_query($conn,$query)){
header('Location: ../index.php');
}
else{
echo "<script>window.alert('Error While Entering the data!.')</script>";
}
}
}
mysqli_close($conn);
?>
In addition I use this global session variable for login page and it works fine.
UPDATE
SIGNUP HTML CODE
<div class="outside">
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-user" for="name"></label>
<div class="control-label col-sm-8">
<input type="text" name="_user" class="form-control" id="name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">
<img class="glyphicon1" src="../assests/at-sign.png">
</label>
<div class="control-label col-sm-8">
<input type="email" name="_email" class="form-control" id="email" placeholder="Enter Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
<div class="control-label col-sm-8">
<input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button name="submit" id="submit" value="Upload" type="submit" class="btn btn-default">Confirm SignUp</button>
</div>
</div>
<p>Already a User? LogIn</p>
</form>
</div>
Use this for Returns the auto generated id used in the last query
mysqli_insert_id($link)
I am working on localhost and trying to create a login system but my session is not starting, here is my php code
if(isset($_POST['_submit'])){
session_start();
if(empty($_POST['_email']) || empty($_POST['_password'])){
echo "<script>window.alert('Invalid User EMAIL or PASSWORD!')</script>";
}
else{
$email = $_POST['_email'];
$password = $_POST['_password'];
$select_user = "SELECT email,password FROM signup WHERE email='$email' AND password='$password'";
$query = mysqli_query($conn,$select_user);
$rows = mysqli_num_rows($query);
if($rows>0){
$_SESSION['user_email'] = $email;
header('Location: ../index.php');
}
else{
echo "<script>window.alert('Invalid User EMAIL or PASSWORD!')</script>";
}
}
}
and after session start i want to change in html code, here is my code
<?php if(!empty($_SESSION['user_email'])){ ?>
<li>Logout</li>
<?php } else { ?>
<li>Login</li>
<li>Sign Up</li>
<?php } ?>
here is my html code of login system
<div class="outside">
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-user" for="email"></label>
<div class="control-label col-sm-8">
<input type="_email" name="_email" class="form-control" id="email" placeholder="Enter Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
<div class="control-label col-sm-8">
<input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button name="_submit" type="submit" class="btn btn-default" >Login</button>
</div>
</div>
</form>
<p>Not a User? Sign Up</p>
</div>
Thanks,
You also need to start the session where you need to work with session
<?php
session_start();
if(isset($_SESSION['somekey']))
{
//code here
}
declare session_start() before the <html> tag. see if it works.
session_start() needs to be outside of your if-statement and is usually always run before any of your PHP code will execute.
session_start();
if(isset($_POST['_submit'])){
Put session_start() at the top of all your PHP files where you want to check any session data. If you have a centralized config file just put it in there.
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";
}
I am building a website in php. The contact us section has four different fields asking input from user. I want to save the data submitted by the user into a text file names contact.txt. The problem is that the submit button in the form does not work at all.
PHP CODE
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$mob=$_POST['mobile'];
$msg=$_POST['message'];
$txt=" Name=$name \n\r Email=$email \n\r Mobile=$mob \n\r Message=$msg \r\n\r\n\r";
$fh = fopen('contact.txt', 'a+');
fwrite($fh,$txt);
fclose($fh);
?>
HTML CODE
<div id="contact-section">
<h3>Fill the form to Register </h3>
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="mobile" class="form-control" required="required" placeholder="Mobile">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email ID">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="4" placeholder="Institute/College/School"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right">Submit</button>
</div>
</form>
</div>
</div
Any help is highly appreciated. Thanks.
Test open success :
if($fh = fopen('contact.txt', 'a+') !== FALSE){
fwrite($fh,$txt);
fclose($fh);
}
Combining my ideas with Fky's here is the most complete code:
<?php
if(!empty($_POST) && array_key_exists('name', $_POST) && array_key_exists('email', $_POST) && array_key_exists('mobile', $_POST) && array_key_exists('message', $_POST)) {
$name=$_POST['name'];
$email=$_POST['email'];
$mob=$_POST['mobile'];
$msg=$_POST['message'];
$txt=" Name=$name \n\r Email=$email \n\r Mobile=$mob \n\r Message=$msg \r\n\r\n\r";
$fh = fopen('contact.txt', 'a');
if($fh = fopen('contact.txt', 'a+') !== FALSE){
fwrite($fh,$txt);
fclose($fh);
}
else {
echo "File can not be opened.";
}
}
else {
echo "Nothing Sent.";
}
?>