How do I make better httpd down checker? - apache

I have apache down checker script (remote server), but I think it doesn't work if httpd has a timeout issue or something similar.
For example, site was offline, but the server was online status.
Should I put timeout stuff or something else? How??
<?php
function GetServerStatus($site, $port)
{
$status = array("OFFLINE", "ONLINE");
$fp = #fsockopen($site, $port, $errno, $errstr, 2);
if (!$fp) {
return $status[0];
} else
{ return $status[1];}
}
?>
<?php
$status = GetServerStatus('xxx.xxx.xxx.xxx',80);
if($status == 'OFFLINE') {
$message = "Server is down now!!";
}
?>

I don't see how this can possibly report incorrectly. You could consider sending a GET / request and timing it out.
You could also consider closing the socket in the success case.

Related

cookies on localhost and hosting

My code is running smoothly on localhost. But it doesn't work on the web server; cookies are not saved.
if (isset($_POST['user-login-form'])) {
$user_mail = strip_tags(trim($_POST['l_email']));
$user_password = strip_tags(sha1(md5($_POST['l_password'])));
$usercontrol=$db->prepare("SELECT * FROM users where user_mail=:user_mail and user_password=:user_password");
$usercontrol->execute(array( 'user_mail' => $user_mail, 'user_password' => $user_password ));
$userresult=$usercontrol->rowCount();
if ($userresult==1) {
$_SESSION['user_mail']=$user_mail;
$_SESSION['user_password']=$user_password;
if(isset($_POST['checkremember'])){
setcookie("user_remember",$user_mail,strtotime("+10 week"));
} else {
setcookie("user_remember",$user_mail,strtotime("-10 week"));
}
echo "2";
exit;
} else {
echo "1";
exit;
}
}
Previously it worked without problems. I installed an SSL certificate and made several changes to the .htaccess file.
But when you type this on a blank page, the cookie is registered.
Code:
setcookie("user_remember", "deneme", time() + (86400 * 30), "/");
echo $_COOKIE["user_remember"];
my problem is solved.
the problem is that the php file is utf-8 BOM.

Using gzip / compression in Symfony 2 without mod_deflate

I am working on two different Symfony 2.8 projects running on different servers. It would like to use compression for faster loading. All resources I found point to mod_deflate. But while the first server does not offer mod_deflate at all, the second server cannot use mod_deflate while FastCGI is enabled.
I only found the information, that one can enable compression within the server (mod_deflate) or "in script". But I did not found any detailed on this "in script" solution.
Is is somehow possible to enable compression in Symfony without using mod_deflate?
You can try to gzip content manually in kernel.response event:
namespace AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class CompressionListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array(array('onKernelResponse', -256))
);
}
public function onKernelResponse($event)
{
//return;
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$encodings = $request->getEncodings();
if (in_array('gzip', $encodings) && function_exists('gzencode')) {
$content = gzencode($response->getContent());
$response->setContent($content);
$response->headers->set('Content-encoding', 'gzip');
} elseif (in_array('deflate', $encodings) && function_exists('gzdeflate')) {
$content = gzdeflate($response->getContent());
$response->setContent($content);
$response->headers->set('Content-encoding', 'deflate');
}
}
}
And register this listener in config:
app.listener.compression:
class: AppBundle\EventListener\CompressionListener
arguments:
tags:
- { name: kernel.event_subscriber }

how to handle error 500 when requesting a distant server with guzzle?

i am requesting a webservice using :
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
try {
$client = new Client();
$response = $client->request('GET', $url); //it crashes at this line
$content = json_decode($response->getBody(), true);
}
catch (ConnectException $e) {
\Drupal::logger('amu_hal')->error('incorrect_url'.$url);
}
today the distant server return a error 500.
How can i modify my code not to crash my site when it happens?
I assume that by distant server you mean a server that takes a long time to connect. You can specify a timeout for the request.
Or perhaps the server returned error 500 and it fails during json_decode? You can check the status code returned by the request.
Or even perhaps the code is failing the line that you indicate but the exception ConnectException is not being caught? Try using Exception as a catch-all to debug this situation.
Instead of using Guzzle directly, I recommend that you use the Drupal wrapper (which uses Guzzle under the hood).
$client = Drupal::httpClient();
$request = $client->get($uri, ['connect_timeout' => 5]);
if ($request->getStatusCode() === 200) {
echo 'Connection Success';
} else {
echo sprintf('Error %d occurred', $request->getStatusCode());
}

WP error connecting to DB

I get this message when I try to access my site.
The site was working properly, and I tried to add google analytics...
Anyway, sodabrasil was a site created out of WP.
sodabrasil.com/paskin was WP.
We did the integration of logins. Everything was working.
When I run http://sodabrasil.com/dbtest.php, it says it's connected.
this is the debug code.
<?php
//connect to MySQL Server
$conn = mysql_connect("localhost", "*****", "******");
if (!$conn)
{
die(mysql_error());
}
else
{
echo "1. Successfully connected to MySQL database server.<br /><br />";
}
// select database
$db_sel = mysql_select_db('*********', $conn);
if (!$db_sel)
{
die ('cant\'t use database: ' . mysql_error());
}
else
{
echo "2. WordPress database selected. <br />";
}
?>
the database * is the DB sodabrasil uses for the login.
But accessing sodabrasil is blocked even before the login!!!
I am not a professional coder, which makes things a lot harder.
This is the real config file:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
define('WP_USE_THEMES', false);
//echo $_SERVER['DOCUMENT_ROOT'];
//die;
//require_once('./blog/wp-blog-header.php');
require_once('/home/houswe5/public_html/sodabrasil.com/paskin/wp-blog-header.php');
global $mysqli;
$server = 'localhost'; // The host you want to connect to.
$username = '****'; // The database username.
$password = '*****'; // The database password.
$database = '*******'; // The database name.
$dbms = 'MySQL 5';
$dbport = '3306';
//$dbh = mysql_connect($server, $username, $password) or die("Unable to connect to SQL server");
//$my_db = #mysql_select_db($database) or die("Unable to select database");
$mysqli = new mysqli($server, $username, $password, $database);
//$mysqli = mysqli_connect($server, $username, $password, $database) or die("Error " . mysqli_error($link));
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;die;
}
// If you are connecting via TCP/IP rather than a UNIX socket remember to add the port number as a parameter.
?>
Exact same credentials.
But this url (http://sodabrasil.com/db_connect.php) throws the error message in regards to DB.
Thank you for you help.
G.

Pubsubhubbub subscriber problem

Pubsubhubbub hub.verify is sync.
But it says me "Error trying to confirm subscription".
Here's my subscribe code:
<?php
if(isset($_GET["hub_challenge"])) {
exit($_GET["hub_challenge"]);;
}
$feeded = $_POST['feed'];
$ch = curl_init("http://pubsubhubbub.appspot.com");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,"hub.mode=subscribe&hub.verify=sync&hub.callback=http://rssreaderbg.net/pubsubbub/example/cssexam/index1.php?url=$feeded&hub.topic=$feeded");
curl_exec($ch);
$conn = mysql_connect("localhost","rssreade_rss","siatsowi");
mysql_select_db("rssreade_rss");
?>
and my callback code:
if(isset($_GET["hub_challenge"])) {
file_put_contents("logmeme1.txt",$HTTP_RAW_POST_DATA,FILE_APPEND);
exit($_GET["hub_challenge"]);
}
Where's my error?
From the spec:
The subscriber MUST confirm that the hub.topic and hub.verify_token correspond to a pending subscription or unsubscription that it wishes to carry out. If so, the subscriber MUST respond with an HTTP success (2xx) code with a response body equal to the hub.challenge parameter.
You may need to explicitly specify a 2xx header. This is the working code I use:
if (isset($_GET['hub_challenge'])) {
header('HTTP/1.1 204 "No Content"', true, 204);
echo $_GET['hub_challenge'];
exit;
}