Apache2 configuration only execute PHP partially - php

I am experiencing trouble while installing Apache2 on my Debian 8 VPS (jessie).
I installed the website correctly on Hostinger, it works perfectly, but knowing Hostinger was a free plan, I moved to a sufficient VPS so I could get my hands dirty and do the job my myself. I now can handle everything, but the truth is, it's been a long time since I didn't use a debian server, and installing Apache seems harder than I thought.
So, I secured my VPS as I wanted, that's not the problem, the site works correctly, but partially.
I mean, on somes pages, the PHP code executes very well, it works with my database without any problem. I have a utils.php file that contains a getBDD() function like this one :
return new PDO("mysql:host=localhost;dbname=name;charset=utf8", "user", "password");
And it works on my main pages, I can request everything. You can see it here : http://mdjfeelzor.ovh/petiteancienne.php (the website is french).
But I also have an ajax chat that needs a bit of PHP to work properly. Aaaand I don't understand why, but when I initialize my $conn with getBDD(), I am experiencing an issue further in the code.
Look at that code :
include '../utils.php';
session_start();
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
try {
$conn = getBDD();
$answer = $conn->prepare('SELECT * FROM openchats WHERE ip=? AND name=?');
$answer->execute(array(stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
stripslashes(htmlspecialchars($_SESSION['name']))));
if (!($data = $answer->fetch())) {
$req = $conn->prepare('INSERT INTO openchats (ip, name) VALUES (:ip, :name)');
$req->execute(array(
'ip' => stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
'name' => stripslashes(htmlspecialchars($_SESSION['name']))));
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
}
It works correctly, right ? (it's supposed to work correctly on your machine) But the thing is, the code starts showing on my web page on the first call of prepare() as you can see here : http://mdjfeelzor.ovh/chat
So, I was wondering, what do you think is the problem ? Is it my way of programming that isn't working ? By the way, I'm using PHP5. I also tried the code <?php phpinfo(); ?> it works perfectly.
Thanks for your help !
EDIT : By the way, the code is shown but it is not supposed to enter in the condition, as $_POST['enter'] doesn't exist on the first load, the code really works on my computer and on Hostinger so I think that the problem comes from Apache configuration ^^' and the AJAX code is not running at that point.

Related

PHP 7 - SFTP keeps interrupting page to load

I'm currently trying to get a file from a server by php sftp. I managed to authenticate and connect to the server. The problem is, that if I want to open a dir on said server, the page just keeps loading until my browser tells me the loading of the page has been interrupted. This only happens, if I try open a dir that EXISTS. If I open a dir that doesn't exist, I get a normal error message.
Therefor I'm not quite sure, wether this is a mistake in my code or a problem with the ftp server.
My Code:
ini_set("display_errors", "1");
$host = "<host>";
$port = 22;
$conn = ssh2_connect($host);
$username = "<user>";
$pub_key = "/home/<user>/.ssh/id_rsa.pub";
$pri_key = "/home/<user>/.ssh/id_rsa";
if (ssh2_auth_pubkey_file(
$conn,
$username,
$pub_key,
$pri_key
)) {
if(!$sftp = ssh2_sftp($conn)){
die("SFTP Connection failed");
};
opendir("ssh2.sftp://".intval($sftp)."/./");
};
Has anyone ever experienced something similar?
I'd be glad for any help :)
~François
It is the expected way.
Opendir return a handle. Your function is working, it's just that you do nothing with the data, and your php script do nothing. It's just waiting with the information
Just handle data, or at least write an echo and it should be ok.
check the manual, there is a working example http://php.net/manual/en/function.opendir.php

PHP script causes page to hang while loading

I've done a lot of coding in the past regarding html and other languages, however only touched the surface of PHP.
I am trying to build an updating server status block on my game server site, and have got my code to a stage where in theory it should work, and my page produces no error messages, however I am experiencing the page to simply hang whenever I try to load it on my site, and if I try to view it locally it displays the PHP code raw as I do not have apache/PHP installed on my local machine.
I have done some testing, and my page loads absolutely fine if I remove or comment out the PHP code blocks, suggesting that my code is causing the error.
Here is the code I am trying to implement in the webpage. I am simply putting it in a <span> statement, and not using include from a file or anything like that.
<?php
$conn = ftp_connect('SERVER');
$file = 'status.txt';
if (#ftp_login($conn, 'USERNAME', 'PASSWORD')) {
ftp_pasv($conn, true);
ftp_get($conn, $file, $file, FTP_ASCII);
$status = file_get_contents($file);
if ($status == 'online') {
echo '<span style="color:#00FF00;">Online</span>';
} elseif ($status == 'offline') {
echo '<span style="color:red;">Offline</span>';
} else {
echo '<span style="color:red;">Unknown</span>';
}
} else {
echo 'Could not authenticate FTP server';
}
ftp_close($conn);
?>
This is my second shot at this code - I have also tried doing the same using file_get_contents('ftp://ADDRESS'); instead of transfering the file from the FTP server, but I still got the page hanging, or displaying a PHP error if it gave one.
( If i leave the page long enough, I also get a 'Server sent no data' or 'Took too long to respond', and tend to get locked out of the website completely; as in getting this same error messages on pages I knew worked fine before, until I reset my IP address.
I have tried the webpage on both Chrome browser and Internet explorer, both browsers hang on the page. )

com token don't work in php

i work with token (COM Object) , in java Script all things is okay . my problem is creating a object from it in php.
this is the code :
try {
// flag 1
$verifier = new COM("pkiactivex.Verifier") or die("Couldn't create the COM Component");
// flag 2
}
catch(com_exception $e)
{
//flag 3
}
first i should say this code work in another local system in wampp and i have xampp and i don't think it's because of xampp or wampp but i think maybe a module is missing. about code in my system code run until flag 1 and even its don't go to exception and flag 3 it's go no where , even i see a blank page with no error !!
i know it's not a php error and i put the code just for more detail!

Unable to call web service function even though it works fine in the test form

I am making a php/c# console app/c# soap service and I have created many functions within the web service, but I am unable to call one of them.
The function I am calling is a function which gets a string value from the database. The call works fine on Windows (using localhost) but when putting it onto a Linux server running under mono I get an exception stating the following:
Function ("getLastResetTime") is not a valid method for this service
The strange this is though, from the Linux server I can access the test form by going to asmx file and run the getLastResetTime function and it returns what is expected, it just seems to be the PHP that can't make the call.
Below is the code that I am using in PHP to call the script
function getLastResetTime()
{
include ("../../config.php");
include ("../../includes/get-settings.php");
include ("../../includes/general.php");
try
{
$client = new SoapClient("http://192.168.1.74/EmailServer/EmailSoapService/EmailSoapService.asmx?WSDL", array("trace" => 1, "exception" => 0));
$result = $client->__soapCall("getLastResetTime", array());
echo "Last Reset: " . $result->getLastResetTimeResult;
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
The screenshot below proves that the web method is working under Mono and what it returns
Below is the code of the web service function
[WebMethod(Description = "Gets the time the Email Server last reset")]
public string getLastResetTime()
{
SoapHandler soapHandler = new SoapHandler();
return soapHandler.getLastResetTime();
}
and below is the code that the web service calls
public string getLastResetTime()
{
try
{
using (ConnectDb db = new ConnectDb(appSettings))
{
string query = "SELECT * FROM settings";
using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
if (reader.GetString("setting") == "app_lastRestart")
{
return reader.GetString("value");
}
}
}
}
}
return "N/A";
}
catch (MySqlException ex)
{
return ex.Message;
}
}
I don't understand why this isn't working, I'm guessing I've probably missing something really simple but can't find it.
Thanks for any help you can provide.
I've just found out the reason, but I don't understand why this doesn't affect the methods.
Basically, I was setting the WSDL to be the IP Address of the server (even though it was local to the server) so I was putting http://192.168.1.74/MySoapService.asmx but if I change it to http://localhost/MySoapService.asmx it works fine.
Why would I need to make it localhost instead of IP address for these services to work.
Is it because I am running several soap requests one after the other so the browser is stopping it, I did notice while I was trying to work out the problem, that Chrome was stating that it was throttling, if this is the issue then I would have thought the connection would time out, not say that the service is invalid.
UPDATE
I've now found out why changing the url from the IP to localhost fixed the issue, I realised as another function started having the same problem and later realised that php by default caches the wsdl, if this option is disabled in the php.ini file and restart the apache server, it then works fine. Probably quite useful in a production environment but a bit of a pain when development.

PHP voting code works on 5.2.5 but not on 5.2.11 anymore

Ok, so a little while back I had some help writing some PHP voting code, it worked just fine after I upgraded my server to use the latest version of PHP. However now I have switched servers, and the PHP isn't as up to date as the other one. Anyways here's my code:
<?php
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}
if($_GET['click'] == 'up1'){
file_put_contents('vote/1u.txt', ((int) file_get_contents('vote/1u.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
Execute and display:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('vote/up1.txt'); ?>
Now when on my other server (PHP version 5.2.5) this code worked great! However on my new server the PHP version is 5.2.11, and because of this the code won't work. My question is, is there any way to make this more compatible with an earlier version of PHP, or to write completely new code that will work just like this one? Or is there a way to tell my servers to use PHP 5.2.5+? I'm using cPanel X admin panel.
I have set the text file permissions to 777 and still nothing!
you are checking for variable "click" but executing the code only if it equals "up1".
But your link tells click to equals "yes" so that part of the code is never true, hence never executed.
Change your executor to this:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('counteru.txt'); ?>
But more logically, your processing code should be rationalized a bit to this:
if the link is clicked :
First, if the data file (lu.txt) does not exist, create it and write '+1' inside of it, else, add 1 to its existing value.
Then, redirects to the initial page.
if($_GET['click'] == 'up1'){
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}else{
$content = file_get_contents('vote/1u.txt');
if(!$content){
die("Error! file_get_content failed !");
}
file_put_contents('vote/1u.txt', ((int)$content) + 1);
}
header('Location: ' . $_SERVER['SCRIPT_NAME']);
}
exit;
Not a bad idea to add a trim() around file_get_contents(). Or to check if $_GET['click'] isset() prior to checking if it's equal to 'up1'.
It's conventional to exit() instead of die() after a header redirect--well, from what I've seen at least.
Basically, during development, turn on error reporting and set your error flag to E_ALL to see everything, including warnings and notices--neither of which halt your code, but should still be known and addressed.
You might discover the reason your code produces different outcomes under different minor versions of PHP by turning on full error reporting.

Categories