Display an "offline" message when a external page is offline (PHP) - php

I have an number of ads displaying on my website that reads the balance of a Blockchain.info bitcoin address but their system for keeping the address balances online keeps going down.
What I want is to do is display a message when the address (not the website) can't be read.
The balance is always between 0 and 10 (and can be a decimal within that range).
I will save you reading all the curl code and just post what I have at the end of my PHP:
if ($BitcoinAddressBalance >= -0 && $BitcoinAddressBalance < 10) {
echo 'Online';
} else {
echo 'Offline';
}
My problem is that the code echoes 'Online' even when there is no data/numbers or I use a unregistered domain.
Can I use curl to check a page on a website has a number between 0 and 10?

If you want to check whether if the value in $BitcoinAddressBalance is not empty then you can use the following:
if ( isset($BitcoinAddressBalance) ) {
echo 'Online';
} else {
echo 'Offline';
}

Related

Insert php snippet after checking correct password

I have a Wordpress page where a user can book language classes. There are different booking options, so I built the page in a way that the booking form charges dinamically from URL. The DOM charges 16 different shortcodes but in front-end it's only displayed the one that user enters in the URL by setting some parameters. The only problem about this is that the page takes a lot of time to charge and display the form.
I'm trying to figure out a way to charge only one shortcode and only set dinamically the value that differs between all shortcodes. After some researching (as my PHP knowledge is limited), I tried this code, and it worked:
function php_insert() {
if( is_page( 645 ) ) {
$awQueryString = $_SERVER['QUERY_STRING'];
parse_str($awQueryString, $awQueryStringParams);
$language = $awQueryStringParams['lang'];
$pack = $awQueryStringParams['pack'];
if (empty($awQueryString)) {
echo 'The URL entered is invalid. Please refresh the page with the correct URL or contact the web admin.';
}
else if ($language == 'english') {
if ($pack == '5') {
echo do_shortcode('[ameliacatalog package=13]');
}
if ($pack == '10') {
echo do_shortcode('[ameliacatalog package=14]');
}
if ($pack == '25') {
echo do_shortcode('[ameliacatalog package=15]');
}
}
}
}
add_action('wp_enqueue_scripts', 'php_insert');
The problem now is that this page is protected by password, and this snippet charges the code at the top of the page and it doesn't wait to check that the password is correct.
I don't know if this is the best way to do this workaround. Also don't know how can I do to solve the password check and location problem.
I hope I have explained everything well.
Thanks

Using Cookie Expire with IF and ELSE PHP, echo not working on ELSE

Why?
I'm attempting to setup an Adword Campaign with my WordPress website, pretty easy stuff but I want to be able to SWITCH contact forms depending if they visited the site using AdWords or Bing/Google SERPS.
So the idea is that if they visit https://example.com/landing-page/ they will reach a page with different contact numbers and email form that has a title indicating that they have come from Adwords, all straight forward, but then, if they click the menu bar away from the landing page, they will get standard numbers and standard email forms, which makes the tracking process a little bit harder.
Setting the temporary cookie
So by using custom WordPress page template files, and when a visitor visits the landing page, it sets a cookie using:
<?php
// 60 Seconds, live environment set to 6000 (1 hour)
$date_of_expiry = time() + 60 ;
setcookie( "adwords-customer", "adwords-visit", $date_of_expiry );
?>
Checking the cookie and do A or B
Then throughout the rest of the website (not present on the landing page) it will check if the cookie is present, and if present it does A, if not it does B, here's the code:
<?php
if(!isset($_COOKIE['adwords-customer']) || ($_COOKIE['adwords-customer'] != 'true')){
echo "cookie set";
} else {
echo "cookie not set";
}
?>
The Problem
The results are always "cookie set" and never else echo "cookie not set", thanks for any help in advance.
You see the ! here?
if(!isset($_COOKIE['adwords-customer']) || ($_COOKIE['adwords-customer'] != 'true')){
^
echo "cookie set";
} else {
echo "cookie not set";
}
It means that you're checking if it is NOT set, so that should be removed or invert the echos.
And the != 'true' make sure you're not checking for boolean. If so, remove the quotes.
Try giving this code a go!
if(isset($_COOKIE['adwords-customer']) && ($_COOKIE['adwords-customer'] == true)){
echo "cookie set";
} else {
echo "cookie not set";
}
If you find it now works but only on the URL that sets the cookie then ensure that your setcookie is set using / to indicate the entire domain, rather than just the /path/, e.g:
$value = 'adwords-visit';
setcookie("adwords-customer", $value);
setcookie("adwords-customer", $value, time()+60);
setcookie("adwords-customer", $value, time()+60, "/");

echo isset with condition

When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.
Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}
if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}
Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';

How to detect if an element is visible using PHP?

So I have a form where 2 out of 6 fields are visible to the user. The user can then click a button to reveal the other fields.
Each field uses the following PHP validation (Note: the preg_match is there to make sure they have entered a space as it's a full name field):
$multipleFormErrors = array();
if (!isset($firstGuestName) || empty($firstGuestName) || !preg_match("/ /",
$firstGuestName)) {
$multipleFormErrors["firstGuestName"] = "You have not entered your full name.";
}
if (!isset($secondGuestName) || empty($secondGuestName) || !preg_match("/ /",
$secondGuestName)) {
$multipleFormErrors["secondGuestName"] = "You have not entered guest #2's full
name.";
}
if (!isset($thirdGuestName) || empty($thirdGuestName) || !preg_match("/ /",
$thirdGuestName)) {
$multipleFormErrors["thirdGuestName"] = "You have not entered guest #3's full name.";
}
And so on up until guest #6.
The results are then being echoed to the user using:
if (isset($_POST["multipleSubmit"])) {
if ($multipleFormErrors) {
echo "<div class=\"errors\">";
echo "Please fix the following errors:";
echo "<ul>";
foreach ($multipleFormErrors as $error) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>";
echo "</div>";
}
}
The issue here is that all of the errors will display even if guests 3 - 6 aren't visible to the user. So If they submit the form with just the initial 2 guests filled out they will get an error because guests 3 - 6 have a value of an empty string. I think a way around this would be for PHP to detect whether the display value is set to block like you can do in JS so is this possible or do I need to do something different?
Cheers!
PHP happens on the server, Javascript happens on the client, and that's the crux of your issue. The server has no way (without a lot more coding and state tracking) to know if the client is looking at something or not.
I recommend:
Keeping your general application structure the way it is (don't do that state tracking, which would require a lot more JS/jQuery/etc)
Perhaps put your error code with the text box, so that the error only shows if the text box does
Code your system with the full realization that guests (beyond the first?) are optional, so guest checking should only occur on server side if there is a partial name (As it is, the error shows if the guest is blank, which will often happen). A blank name for guests 2-6 is probably completely legitimate.

What is the best way to determine if my users can accept cookies/sessions

I need some help with some logic for my buying process on my website.
We have a 4 step buying process: results, customer details, payment details, order confirmation.
The results page simply outputs prices to the screen based on some query string parameters.
I then save lots of information to PHP Sessions variables for later use.
On the 2nd stage, the customer stage, I want to output some of these session variables to the screen which for the most part works.
In my code, one of the first things I do is check the existence of one of the session variables I set on the results page, just to check we are in business and the customers quote info is saving properly.
I have set up warning emails to myself to notify me when a user lands on either the customer or payment stage of the booking process but apparently the first session variable does not exist. I then display a friendly error message asking if they have enabled cookies in their browser.
We seem to be getting a lot of these warnings emails, alarmingly high. It doesn't feel like an accurate statistic of how many customers could arrive without cookies enabled.
The email alerts me of the current URL, the ref URL if there was one, the users IP address, and an output of all Session Vars they have saved (always none of course!)
I'm just stumped what to do next - are these really users or bots hitting the results page without cookies enabled which means they'll fail the test on the next page or could it be something else?
I have session_start() on the top of each of these buying pages so it's nothing like that.
Here's my customer page:
<?php
require_once "../includes/common.php";
$quoteShared = new quoteShared();
// Check if this is a direct page hit
if (requestSession("sessionnumber") == "") {
echo $quoteShared->directHit();
die;
common.php has session_start() at the top.
function requestSession($xParam) {
$value = "";
if (isset($_SESSION[$xParam]))
{
if ($_SESSION[$xParam] != "") {
$value = $_SESSION[$xParam];
}
}
return $value;
}
You can do it in javascript also, this way :
function cookiesAreEnabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? 1 : 0;
if (typeof navigator.cookieEnabled == "undefined" && cookieEnabled == 0){
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("test­cookie") != -1) ? 1 : 0;
}
return cookieEnabled == 1;
}
BEST WAY PHP
<?php
session_start();
if(!isset($_GET['testing'])){
setcookie('cookietest', 'somevalue', time()+3600);
header("location: cookie.php?testing=1");
}else{
if(isset($_COOKIE['cookietest']) && $_COOKIE['cookietest'] == 'somevalue'){
echo 'cookie enabled';
}else{
echo 'cookie not enabled';
}
}

Categories