I had this working, then out of the blue something changed and my php captcha fails constantly. I feel like it could be a server side issue, but I can't find anything wrong.
I'm using
https://www.phpcaptcha.org/
https://github.com/dapphp/securimage
I'm testing this at the root level of the site.
<?php
session_start();
require_once('securimage/securimage.php');
?>
<form class="" method="post" action="" enctype="multipart/form-data" id="captcha">
<?php
$options = array();
$options['namespace'] = 'captcha';
$options['input_id'] = 'captcha_code';
echo Securimage::getCaptchaHtml($options, Securimage::HTML_IMG);
echo Securimage::getCaptchaHtml($options, Securimage::HTML_ICON_REFRESH);
echo Securimage::getCaptchaHTML($options, Securimage::HTML_INPUT);
?>
<input type="submit" value="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$captcha_code = isset($_POST['captcha_code']) ? $_POST['captcha_code'] : null;
$image = new Securimage();
var_dump($_SESSION);
if($image->check($captcha_code) == true)
{
echo "Captcha was good!";
}
else
{
echo "Captcha did NOT match!";
}
}
?>
The captcha $SESSSION[]'s get set properly.
array(4)
{
["securimage_code_disp"]=> array(1)
{
["captcha"]=> string(6) "LG2BE2"
}
["securimage_code_value"]=> array(1)
{
["captcha"]=> string(6) "lg2be2"
}
["securimage_code_ctime"]=> array(1)
{
["captcha"]=> int(1457447465)
}
["securimage_code_audio"]=> array(1)
{
["captcha"]=> NULL
}
}
But no matter what i try, the if condition always fails. Returns False.
if($image->check($captcha_code) == true)
{
echo "Captcha was good!";
}
else
{
echo "Captcha did NOT match!";
}
Everything is working except the validation part. It refuses to work.
Related
I have this "safe" project to get the basics down just to become more familiar with OOP.
Here is my code:
chest.php
<?php
class safe{
public $isLocked = true;
public $isClosed = true;
public function unlock(){
if ($this->isLocked == true) {
$this->isLocked = false;
echo "Safe Unlocked<br>";
}else{
echo "Safe Already Unlocked<br>";
}
}
public function lock(){
if ($this->isLocked == false) {
if ($this->isClosed == true) {
$this->isLocked = true;
echo "Safe Now Locked<br>";
}else{
echo "Safe Needs To Be Closed To Be Locked<br>";
}
}else{
echo "Safe Is Already Locked<br>";
}
}
public function checkLock(){
if ($this->isLocked == true) {
echo "<span style='color:red;'>Safe Is Locked</span><br>";
}else{
echo "<span style='color:red;'>Safe Is Not Locked</span><br>";
}
}
public function open(){
if ($this->isClosed == true) {
if ($this->isLocked == false) {
$this->isClosed = false;
echo "Safe Is Now Open<br>";
}else{
echo "Safe Is Locked, Cannot Open<br>";
}
}else{
echo "Safe Already Open<br>";
}
}
public function close(){
if ($this->isClosed == false) {
if ($this->isLocked == false) {
$this->isClosed = true;
echo "Safe Now Closed<br>";
}else{
echo "Safe Is Currently Locked Cannot Close<br>";
}
}else{
echo "Safe Already Closed<br>";
}
}
}
index.php
<?php
require 'chest.php';
$safe = new safe();
if ($safe->isLocked == true) {
echo "Safe Currently Locked<br><br>";
}
$safe->open();
$safe->lock();
$safe->unlock();
$safe->open();
$safe->lock();
$safe->open();
$safe->unlock();
$safe->checkLock();
$safe->lock();
$safe->close();
$safe->lock();
$safe->open();
$safe->lock();
$safe->checkLock();
if (isset($_POST['unlock'])) {
$safe->unlock();
}
if (isset($_POST['lock'])) {
$safe->lock();
}
if (isset($_POST['open'])) {
$safe->open();
}
if (isset($_POST['close'])) {
$safe->close();
}
if (isset($_POST['check'])) {
$safe->checkLock();
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Safe</title>
</head>
<body>
<form method="post">
<input type="submit" name="unlock" value="Unlock">
<input type="submit" name="lock" value="Lock">
<input type="submit" name="open" value="Open">
<input type="submit" name="close" value="Close">
<input type="submit" name="check" value="Check Lock">
</form>
</body>
</html>
It works fine when I call the functions without the if statement, but when I try to put them into an isset($_POST['value'] it keeps displaying only 1 output and always sets isLocked(); back to being true so it never works.
To stop the page from refreshing each time would require javascript and ajax. Your javascript could stop the page from loading on click, and your ajax could make a call to the server to execute your PHP
<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>
I have this code but it does not seem to be working. After entering an IP into the text field and hitting the button it will return with the page loaded up to the php code but will not load the php code result nor any code past it.
<?php
if(isset($_POST['resolve'])){
$api = "https://iphub.p.mashape.com/api.php?ip=";
$endbit = "&showtype=4";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
// assuming $_POST['resolve'] has an IP
// $response = file_get_contents($api.$_POST['name'].$endbit);
$response = Unirest\Request::get($api.$_POST['name'].$endbit,
array(
"X-Mashape-Key" => "U3sk6nAZzBmshyiHEUZHigxLGp1ZTZnKjsnkd8LWULJmRRp",
"Accept" => "application/json"
)
);
?>
<div align="center">
<?php
if($response['proxy'] == '0') {
echo 'No';
}
elseif($response['proxy'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
?>
</div>
<?php
}
}
?>
The API I am using requires me to have the header/X-Mashape-Key.
So, I have this code
<?php
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
}
function step1()
{
if(isset($_POST["step1_submit"]))
{
step2();
}
else
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
}
function step2()
{
echo "reached step 2";
if(isset($_POST["step2_submit"]))
{
echo '<br/>'.$_POST["step2_input"];
step3();
}
else
{
echo '<form method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
}
function step3()
{
echo '<strong>WELL DONE</strong>';
}
?>
It shows the input for step1, but never gets to show step2. In my opinion, it gets stuck on calling the function step2, because while doing it, the isset($_POST(step1_submit)) changes it value to NULL.
How can I manage to make this code work? It should be like: filling step 1 input >> getting to fill step 2 input >> submit step 2 >> get to see the 'WELL DONE' echo.
Basically the conditional statement on top needs an else branch:
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
} else {
step2();
}
...
However, if you have more steps it should look like this:
switch(TRUE) {
case isset('step1_submit') :
step2();
break;
case isset('step2_submit') :
step3();
break;
...
default:
echo "Fill step1!";
step1();
}
Then change your functions to:
function step1()
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
function step2()
{
echo '<form action="" method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
...
The if conditionals aren't required there.
Here is the code i made, im expecting it to work but somewhere there must be an error. I can't figure out myself, Please help.
<?php
if(isset($_POST['submit'])){
$max_size = 500000;
$image_upload_path = "images/products/";
$allowed_image_extension = array('jpg','jpeg','png','gif');
for($i=0;$i<2;$i++)
{
//check if there is file
if((!empty($_FILES['image[]'][$i])) && ($_FILES['image[]']['error'][$i]==0))
{
//check extension
$extension = strrchr($_FILES['image']['name'][$i], '.');
if(in_array($extension,$allowed_image_extension))
{
//check file size.
if($_FILES['image']['size'][$i] > $max_size)
{
echo "file too big";
}
else if($_FILES['image']['size'][$i] < 1)
{
echo "file empty";
}
else
{
//we have pass file empty check,file extension check,file size check.
$the_uploaded_image = $_FILES['image']['tmp_name'][$i];
$the_uploaded_image_name = $_FILES['image']['name'][$i];
//replace empty space in filename with an underscore '_'
$the_uploaded_image_name = preg_replace('/\s/','_',$the_uploaded_image_name);
//get the file extension
$the_uploaded_image_extension = explode(',',$the_uploaded_image_name);
$the_new_image_name = $the_uploaded_image_name."".md5(uniqid(rand(),true))."".$the_uploaded_image_extension;
$save_image_as = $the_new_image_name;
//check file exist
if(file_exists($image_upload_path."".$the_new_image_name))
{
echo "file".$image_upload_path."".$the_new_image_name." already exist";
}
else
{
if(move_uploaded_file($the_uploaded_image,$save_image_as))
{
echo "image".$the_uploaded_image_name." uploaded sucessfully";
//set the image path to save in database column
}
else
{
echo "there was an error uploading your image.";
}
}
}
}
else
{
echo "extension not allowed";
}
}
else
{
echo "please choose file to upload";
}
}
}
?>
<html>
<head><title>image upload</title></head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image[]"/>
<input type="file" name="image[]"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
This is my new PHP code . Im getting both the result as found found not found not found. Will someone tell me what am i doing wrong here. The if else condition is seems to be not working as both the conditions are giving ouput. Why?
<?php
if(isset($_POST["submit"])) {
echo $_POST["submit"];
echo "<br/>";
for($i=0;$i<count($_FILES['image'])-1;$i++)
{
if(!empty($_FILES['image']['tmp_name'][$i]))
{
echo "found";
echo "<br/>";
}
else
{
echo "not found";
echo "<br/>";
}
}
}
else
{
echo "form is not posted";
}
?>
I guess the obvious WTF would be $_FILES['image[]'][$i], which should just be $_FILES['image'][$i] (the [] in the name makes it an array, it's not part of the name).
I'm unwilling to troubleshoot anything beyond this for you without more information. Try this at various points in the code:
echo '<pre>';
var_dump($_POST); // or other variables
echo '</pre>';
This should help you to debug your own code, something you must learn to do.