When Including a File, Page Breaks - php

I'm experiencing a very odd problem. Everything works as expected on my local host. When I upload to a live server, the page just cuts off right where I'm including a file. Just white space beneath it. Nada...
The line that breaks is:
<? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?>
And the file being included is:
<?php
/*///////////////////////////////////////////////////////////////////////
Part of the code from the book
Building Findable Websites: Web Standards, SEO, and Beyond
by Aarron Walter (aarron#buildingfindablewebsites.com)
http://buildingfindablewebsites.com
Distrbuted under Creative Commons license
http://creativecommons.org/licenses/by-sa/3.0/us/
///////////////////////////////////////////////////////////////////////*/
function storeAddress(){
// Validation
if(!$_GET['email']){ return "No email address provided"; }
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
return "Email address is invalid";
}
require_once('MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('xxxxxxx');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "xxxxxx";
if($api->listSubscribe($list_id, $_GET['email']) === true) {
// It worked!
// return 'Success! Thank You!';
echo '<script> window.location.href = "thank-you.php"; </script>';
}
else
{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
The only thing edited in the above code is the API key and List ID.

its because is missing parentesis on your if condition.
require_once('inc/store-address.php');
if($_GET['submit'] **)** {
echo storeAddress();
}

It seems there was an error. It was working on my local server because there wasn't an error.
I was using filezilla do upload my content. For some reason it appears to be an encoding issue when uploading.
I don't know if I should delete this question or answer to help someone else with the problem later on so I chose the later.
I manually uploaded my file and guess what, it works!

Related

Cloud hosting causing issues with JSON retrieval?

I'm hosted with Smarthosting, they used cloud based hosting which delivers faster loading times - great!
But I have a snag.
I'm setting some sessions via PHP in a seperate file...
<?php
session_start();
if(filter_var($_POST['question_1'], FILTER_VALIDATE_INT)) {
$_SESSION['question_1'] = addslashes($_POST['question_1']);
}
if(filter_var($_POST['question_2a'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2a'] = addslashes($_POST['question_2a']);
}
if(filter_var($_POST['question_2b'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2b'] = addslashes($_POST['question_2b']);
}
if(filter_var($_POST['question_2c'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2c'] = addslashes($_POST['question_2c']);
}
if(filter_var($_POST['question_2d'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2d'] = addslashes($_POST['question_2d']);
}
if(filter_var($_POST['question_2e'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2e'] = addslashes($_POST['question_2e']);
}
if(filter_var($_POST['question_2f'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2f'] = addslashes($_POST['question_2f']);
}
if(filter_var($_POST['question_2g'], FILTER_VALIDATE_INT)) {
$_SESSION['question_2g'] = addslashes($_POST['question_2g']);
}
?>
Then later on I access another PHP file which puts these into a JSON string...
<?php
session_start();
echo json_encode($_SESSION);
?>
This works fine, however, until I call the JSON via Ajax...
$.getJSON( "retrieve-variables.php", function( data ) {
var items = [];
...etc....
});
It's not pulling the most recent session data, it seems to pull back the session data from previous attempts. Is this to do with the cloud hosting? Or some other issue? Is there a way I can disable caching for this particular file and/or entire directory?
Thanks for listening.
EDIT: If I access the PHP retrieval file directly, then hard refresh it (CTRL+F5), and then go through the form again, it will ignore the answers I've selected and enter the data for that hard refresh I did
I found out how to fix this, in case anyone stumbles upon this post.
I simple added cache:"false" to the ajax get request.

google no captcha still shows text

I have this code in my form:
<div class="g-recaptcha" data-sitekey="MY_KEY"></div>
And the validation part:
// Re captcha part
if (isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
if (!$captcha) {
redirect('login', 'refresh');
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=MY_SECRET_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
// t($response,1);
if($response."success" == false) {
redirect('login', 'refresh');
}
It sort of works now, this is my result:
When I click the checkbox, I still need to type the text for verification.
I thought that clicking the checkbox was enough?
Am I doing something wrong here?
Here's how I check "No Captcha" response and it always shows me "one click" verification box, even in localhost:
$g_recaptcha_response = filter_input(INPUT_POST, 'g-recaptcha-response');
if (isset($g_recaptcha_response)) {
$google = 'https://www.google.com/recaptcha/api/siteverify?secret='
. ZorAuthConfig::RC_SECRET_KEY
. '&response=' . $g_recaptcha_response;
$google_json = file_get_contents($google);
$google_data = json_decode($google_json);
// Check reCaptcha response.
if (!$google_data->success) {
// Do something if robot.
} else {
// Do something if human.
}
}
I implemented this in small library I wrote recently, so you can check it out if you want more details: https://github.com/zoran-petrovic-87/ZorAuth
http://zoran87.blogspot.com/2014/12/zorauth-10b-complete-flexible-no.html
The reCaptcha will verify at first if you are a bot or not by analyzing your mouse movements (and other stuff).
In case the test failed, it will popup a box where you need to write the 2 words shown in the image.
This is all fine and normal, but this is only a client side validation, if you need to make sure that validation was truly successful and not hacked in some way, or whatever... Then you submit the form which will contain the g-recaptcha-response like the way you did, then you will receive the final confirmation from Google in that JSON string.

Login Verification , $_GET['id'] in php not working

THIS is the PHP code m using for checking the values
if($uname==$row['username']) {
if($uname=='' || $pass=='') {
header("Location:login.html?id=Some fields are empty");
} else if($uname==$row['username'] && $pass==$row['password']) {
header("Location:1.html?id=$uname");
} else {
// **HERE I AM REDIRECTING TO THE LOGIN PAGE AND SENDING ERROR MESSAGE AS ID**
header("Location:login.html?id=Incorrect Password");
}
}
In the HTML part I included this to show the error message
<?php
if(isset($_GET['id']))
{
echo $_GET['id'];
}
?>
However NOTHING is getting printed
As mentioned in the comments, make sure that the server is set to parse .html file as PHP. See Server not parsing .html as PHP
The other option is to change your login.html to be login.php and go from there.
you can use php filters to check correctly if the values are set or not like the following :
filter_has_var(INPUT_GET , 'id');
this function returns true or false

Another command to replace exit in php?

is there any any other command we can use as an alternative to exit(); in php.
Because it breaks my html code at the end of the page if the condition is not met and when script has to exit.
Or if anyone has any other idea to resolve this issue???
Thanks
Update:
html code...
<?php
if username is not in correct format
echo "Please check your username";
exit();
if Username and Password didn't match
echo "Wrong Username or Password.";
exit();
if some other condition not met
echo "Condition not met";
exit();
?>
html code continues...
Now the problem is if any of the condition is not met and the script has to exit, the html code below it, which is a whole webpage, does not display...
And please...I am not a computer geek, had a problem so asked it, but why people vote down the question??? don't understand....
You should probably wrap your code into an if statement:
<?php
if($code == 'ok'){
echo 'ok';
} else {
echo 'not ok';
}
?>
your script doesn't have to exit(), you can add statements where you want and how you want.
As the name suggests, the PHP exit() statement will cause your PHP script to exit, right there and then, and not do anything else. If you want it to carry on processing the rest of the code, just don't use exit().
Looking at your code, what you seem to be aiming for is displaying errors to the user, and then (I would guess) re-showing the form they filled in incorrectly.
Rather than just echoing the errors as soon as you discover them, why not store them into a variable, which can then be displayed at an appropriate point in the HTML? Even the most basic of scripts can benefit from a bit of basic code structure.
As an example (and I stress this is not the One True Pattern for this kind of thing), you could arrange your file something like this:
if ( /* form has been submitted */ )
{
$errors = validate_form();
if ( count($errors) > 0 )
{
display_form($errors);
}
else
{
display_success_message();
}
}
else
{
display_form();
}
function validate_form()
{
$errors = array();
// Series of if conditions, each adding a message to $errors if appropriate
return $errors;
}
function display_form($errors=array())
{
// HTML <ul> list displaying the contents of $errors, if any
// HTML for form
}
function display_success_message()
{
// HTML thanking user for a successful form submission
}

How to detect page redirect with PHP?

I'm trying to detect whether an image exists on a remote server. However, I've tried several methods and can't get any of them to work.
Right now I'm trying to use this:
if (!CheckImageExists("http://img2.netcarshow.com/ABT-Audi_R8_2008_1024x768_wallpaper_01.jpg")) {
print_r("DOES NOT EXIST");
} else {
print_r("DOES EXIST");
};
function CheckImageExists($imgUrl) {
if (fopen($imgUrl, "r")) {
return true;
} else {
return false;
};
};
But it returns 'true' whether the image actually exists or not (the above image should, but change it to gibberish and it still will return 'true'). I have a feeling it could be because if the URL does not exist, it redirects to the homepage of the site. But I don't know how to detect that.
Thanks for any help!
Use cURL.
After fetching the resource, you can get the error code calling curl_errno().
The chances are you are getting a HTML page back into your $imgUrl that contains "404 image not found" or something similar.
You should be able to check the response for a code indicating that the request failed or redirected.
This should do the trick (using image size):
if (!CheckImageExists("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png")) {
echo 'DOES NOT EXIST';
} else {
echo 'DOES EXIST';
};
function CheckImageExists($imgUrl) {
if (#GetImageSize($imgUrl)) {
return true;
} else {
return false;
};
};
Got it working with Seb's method. Just used YQL to inspect the actual content of the page and determine if it's an error or not.

Categories