php form action with recaptcha check - php

Working on a simple contact form, and I'm trying to integrate Google ReCaptcha to eliminate the spam I started receiving.
Right now it's checking to see if the ReCaptcha is successful. If it's not, it just currently reloads the page and resets the form. If it is, I'd like it to fire the receiving contact form php page and pass the form values along with it.
It's getting hung up on that part. Any help would be greatly appreciated.
<?php
// grab ReCaptcha library
require_once "recaptchalib.php";
// your secret key
$secret = "XXXXXXXXXXXXXXXXXXXXXXXX";
// empty response
$response = null;
// check secret key
$reCaptcha = new ReCaptcha($secret);
// if submitted check response
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
?>
<!DOCTYPE html>
<html>
<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 Form</title>
<link href='https://fonts.googleapis.com/css?family=Work+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="assets/form-basic.css">
</head>
<body>
<div class="main-content">
<?php
$action = '';
if ($response != null && $response->success) {
$action = "contactengine.php";
} else {
?>
<form class="form-basic" action="<?php echo $action ?>" method="post">
<div class="form-title-row">
<h1>Get in Touch</h1>
</div>
<div class="form-row">
<input type="text" name="Name" placeholder="Name" required>
</div>
<div class="form-row">
<input type="email" name="Email" placeholder="Email" required>
</div>
<div class="form-row">
<textarea name="Message" placeholder="Message" required></textarea>
</div>
<div class="form-row">
<div class="g-recaptcha" data-sitekey="fdgafafdadfgsdfgrafgaRlifgsfgserysrtyc3H"></div>
</div>
<div class="form-row">
<button type="submit" name="submit" value="submit">Send</button>
</div>
</form>
<?php } ?>
</div>
<script src='https://www.google.com/recaptcha/api.js?hl=en'></script>
</body>
</html>

You can either move the captcha checking code into the receiving contact form page, or add the contact form processing logic in the main page. You probably want to use the last approach.

Related

php code works fine on xampp localhost but not working on live server

I have uploaded all the php files on live server same as on localhost. Few files works fine. But my registration page is not working when i try to register it does not store data in phpmyadmin. and shows the error as below.
enter image description here
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Event Management</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<!--<link href="css/style.css" rel="stylesheet"/>-->
<link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">
<!--<link href="css/bootstrap.css" rel="stylesheet"/>-->
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<!-- Database file include -->
<?php require_once ('main.php'); ?>
<!-- Database file include End -->
<div class="container justify-content-center">
<h2 align="center">Log In or Sign Up</h2>
<div class="container">
<form method="post" action="main.php">
<div class="row justify-content-center">
<div class="col-6">
<input type="text" class="form-control" placeholder="Name" name="name"/>
<label></label>
<label></label>
<input type="text" class="form-control" placeholder="username" name="username"/>
<label></label>
<label></label>
<input type="password" class="form-control" placeholder="password" name="password"/>
<label></label>
<label></label>
<input type="email" class="form-control" placeholder="email" name="email"/>
<label></label>
<label></label>
<input type="text" class="form-control" placeholder="contact" name="contact"/>
<label></label>
<label></label><br />
<button class="btn btn-info" id="register" name="register">Register</button>
</div>
</div>
<div class="row justify-content-center">
<div class="col-6">
<label>Already have an Account?</label>
Login Here.
</div>
</div>
</form>
</div>
</div>
</body>
</html>
main.php file code for registration
if(!empty($_POST['name']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['email']) && !empty($_POST['contact']))
{
$Name = $_POST['name'];
$uName = $_POST['username'];
$pass = $_POST['password'];
$mail = $_POST['email'];
$contact = $_POST['contact'];
$date = date('d-m-yy');
$mysqli->query("insert into register(Name,username,password,Email_Id,Phone_No,
date_Created) values('$Name','$uName','$pass','$mail','$contact','$date')") or die($mysqli->error);
$mysqli->query("insert into users(email,user_name,pass_word) select r.Email_Id, r.username, r.password from register r where r.Email_Id NOT IN (SELECT email FROM users);") or die($mysqli->error);
echo "Data inserted successfully!";
header('location: login.php');
}
You've an error on main.php line number 73.
Undefined variable: mysqli
Please show more to view & get your solution.
Thanks

How do I include array from another PHP file

In the handle_signup.php page, I try to handle with the data which was sent from the sign up page. If the nickname blank has no value, then the error message is "Please enter your nickname". So, in the signup page, I try to include the $errorMsgs array from handle_signup.php page and show the msg below the nickname input. How can I get the array from the handle_signup.php page? Currently, I use include_once, but there is no message in the array after transferring to handle_signup.php. Could some one help this issue? Thank you.
Handle the value from signup.php
<?php
require_once('./conn.php');
$errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');
if(isset($_POST['submit'])) {
if(empty($_POST['nickname'])) {
$errorMsgs['nickname'] = "Please enter your nickname";
header("Location: ./signup.php");
}
};
?>
Sign up page - signup.php
<?php
include_once 'handle_signup.php';
var_dump($errorMsgs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<title>Message Board - Sign Up</title>
</head>
<body>
<div class="container_sign">
<h1 class="title">Create Account</h1>
<form class="sign" method="POST" action="./handle_signup.php">
<div>
<i class="far fa-user"></i>
<input type="text" placeholder="Nickname" name="nickname">
</div>
<p class="warning__msg"></p>
<div>
<i class="far fa-envelope"></i>
<input type="text" placeholder="Email" name="email">
</div>
<p class="warning__msg"></p>
<div>
<i class="fas fa-lock"></i>
<input type="password" placeholder="Password" name="password">
</div>
<p class="warning__msg"></p>
<input type="submit" value="SIGN UP" name="submit">
</form>
</div>
</body>
</html>
As DarkBee suggested, you can use $_SESSION for your error messages, or simply don't use header("Location: ./signup.php") but include all your PHP code in the same page.

Failed to login even credentials are exactly right in laravel login

I made a login form, while I used to enter credentials, my email and password are absolutely correct, but this code shown me an error, that I defined in else condition. please help me to find my mistake.
loginstudent.blade.php (login form)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Login</title>
</head>
<body>
<div class="container">
<table class="table table-striped">
<form action="{{route('matchlogin')}}" method="post">
#csrf
<div class="container">
<label for="uname"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required/> <br/>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="pwd" required/> <br/>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" name="submit" value="Login"/>
</form>
</table>
</div>
#if(\Session::has('notmatch'))
<div class="container">
<h2> {{\Session::get('notmatch')}} </h2>
</div>
#endif
</body>
</html>
studentcontroller.php (controller function)
public function matchlogin(Request $request)
{
$email = $request->email;
$password = $request->pwd;
if (Auth::attempt(array('email' => $email, 'password' => $password))) {
return "success";
} else {
$request->session()->flash('notmatch', 'Email & password not match');
return redirect(route('loginstudent'));
}
}
web.php (Routes)
Route::get('/loginstudent','studentcontroller#login')->name('loginstudent');
Route::post('/checklogin','studentcontroller#matchlogin')->name('matchlogin');
How did you save your password? Is it hashed and then saved? Because Auth::attempt matches the hashed pattern. If your password is saved in simple text or in some other encryption form, it won't work and will give mismatch.
Thanks

Cannot get login to redirect to home page if the user is registered on database

I cannot seem to link my login page to my home page, any help would be much appreciated. I know its connecting to the database as i can create users but for some reason its not letting me sign in with a created user.
<?php require ("insert.php"); ?>
<?php
if(isset($_POST[ 'Login' ]))
{
$email= mysqli_real_escape_string($con, $_POST ['email']);
$pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);
$result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' ");
$row = $result->fetch_array(MYSQLI_BOTH);
session_start();
$_SESSION["User ID"] = $row['UserID'];
header ('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>AMSadler login</title>
<meta charset="utf-8">
<meta name="description" content="description of webpage">
<meta name="keywords" content="keywords go here">
<meta name="author" content="Anthony">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/login.css">
<link rel="index" href="index.php">
<link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
<div class="header">
<div id="logo">
<img src="img/logo.png" alt="logo" title="AMSadler.com"/>
</div>
<div id="signup">
<button type="button">Sign up</button>
</div>
</div>
<div id="login">
<form>
<input type="text" name="email" placeholder="Email address">
<br>
<input type="password" name="password" placeholder="Password">
<br>
<input id="Login" type="submit" name="Login" value="Login">
</form>
</div>
<footer>
<div id="copyright">
<p>&copy Copyright 2015</p>
</div>
</footer>
</body>
</html>
You need to change your form tags.
From,
<form>
To,
<form action="yourPageName.php" method="post">
You need to specify your method type so in your PHP code you can get it using that method. If you leave out the method part it by default uses $_GET. Seeing as your code is pointing to $_POST then you set it to method="post".
You also need to set action="" this can be set to # for same page or leave it blank or using a file name. This will redirect the form to that page.

PHP Session variable not carrying over

I am working with PHP session variables, but the problem is that the session variables are not carrying over. I checked using Firebug, and found that the session variables are not being created at all.
print_r($_SESSION); returns an empty array after the page returns to index.php, but displays it correctly before the refresh.
logincheck.php:
<?php
session_start();
error_reporting(-1);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
{
$username=$_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Smart Shopping Cart System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<style type="text/css">
.show-grid{
background:#E0E0E0;
}</style>
</head>
<body>
<?php
include "connection.php"
?>
// ** Some Content**
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Log In | SmartCart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-brand">
SmartCart
</div>
</div>
</div>
<div class="container">
<br />
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<form role="form" method="post" name="login" action="test.php">
<div class="form-group">
<label for="username">Operator Username</label>
<input type="username" class="form-control" name="username" placeholder="Enter your username">
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-default" name="sub">Submit</button>
</form>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body></html>
<?php
}
?>
test.php
<?php
session_start();
error_reporting(-1);
?>
<html><head><title>Redirecting...</title>
</head>
<body>
Redirecting....
<?php
include "connection.php";
if(isset($_POST['sub']))
{
$u_name=$_POST['username'];
$password=mysql_escape_string($_POST['pass']);
$password=mysql_escape_string($password);
$w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'";
$b=mysql_query($w) or die(mysql_error()."in query $w");
$num_rows = mysql_num_rows($b);
if($num_rows > 0)
{
$_SESSION['logged_in']=true;
$_SESSION['username']=$_POST['username'];
print_r($_SESSION);
echo '<meta http-equiv="refresh" content="0; url=index.php">';
}
else
{
echo "<script>
alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>";
}
}
?>
</body></html>
Please help.
Spot the difference:
logincheck:
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
^^^^^^^^^
test:
$_SESSION["loggedin"]=true;
^^^^^^^^
One of these things is not like the other...
I think the under score is missing in on test.php page
in $_SESSION['loggedin']=true;
change this to $_SESSION['logged_in']=true;
As you are using this in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
there is no error with you session part the error is may be in you db access part check your database connection. you can check you session by commenting the query part, it is working fine.

Categories