PHP not including connection info, what's wrong? - php

I'm trying to roll a CMS website and I'm on 1and1 Internet's hosting. I'm trying to connect to my MySQL database and I get the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
After some frustration, I decided to check my include and it turns out that the following code is not including my connection variables file.
admin.php
<?php
include("connection.php");
$link = mysql_connect($connection_server, $connection_user, $connection_password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
connection.php
<?php
/* --------------------------------------
/
/ Bitachon.org Connection Variables
/
/ Created by: Moshe [Last Name]
/
/
/ Date: October 12, 2010
/
/
/ This file contains the connection
/ variable to connect to the MySQL
/ database that powers [site]
/
/ --------------------------------------*/
//Variable to confirm that the file has been included
$connnections_included = true;
/* --- Connection Variables ---*/
$connnection_server = "[server]";
$connection_database = "[db]";
$connection_user = "[username]";
$connection_password = "[password]";
?>
What's wrong?

The problem lies in your connection.php file, where there is a typo:
$connnection_server = "[server]";
// ^-- third n
Fixing that (along with the include() issue mentioned in Josh's answer) should resolve your problem.

Verify that you have the path set correctly
include("/path/to/connection.php");
Check permissions on connection.php, test to see if it's readable
$filename = 'connection.php';
if(is_readable($filename)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}
Is the MySQL Database on the same server? AKA Localhost or another server?
Hard code the path
$pwd = `pwd`;
echo "PWD: ".$pwd."<br />"; // use just for testing
include($pwd."/connection.php");
EDIT:
Can you compare connection.php and admin.php
$filename = 'admin.php';
echo "Permissions: ".substr(sprintf("%o",fileperms($filename)),-4)."<br />";
echo "File Owner: ".fileowner($filename)."<br />";
echo "File Group: ".filegroup($filename)."<br />";
if(is_executable($filename)) {
echo ("$filename is executable<br />");
} else {
echo ("$filename is not executable<br />");
}
if(is_readable($filename)) {
echo "$filename is readable<br />";
} else {
echo "$filename is not readable<br />";
}
echo "Real Path: ".realpath($filename)."<br />";
$filename = 'connection.php';
echo "Permissions: ".substr(sprintf("%o",fileperms($filename)),-4)."<br />";
echo "File Owner: ".fileowner($filename)."<br />";
echo "File Group: ".filegroup($filename)."<br />";
if(is_executable($filename)) {
echo ("$filename is executable<br />");
} else {
echo ("$filename is not executable<br />");
}
if(is_readable($filename)) {
echo "$filename is readable";
} else {
echo "$filename is not readable";
}
echo "Real Path: ".realpath($filename)."<br />";

Put error_reporting(E_ALL); before
mysql_connect(). Do you get notices
about undefined variables? –
Lekensteyn
#Lekensteyn - Yes, I do. – Moshe
Put the following in config.php, before $connections_included.
global $connections_included, $connection_server, $connection_user, $connection_password;
It'll export those variables to the global scope.

After some frustration, I decided to check my include and it turns out that the following code is not including my connection variables file
To determine if this is really the case, try the following:
In connection.php add the line: die('This is connection.php.'); See if the script dies. If so, the file is being included.
Before $link = mysql_connect($connection_server, $connection_user, $connection_password) add: var_dump($connection_server) and see if the connection server is output when you run the script, or if something like "NULL" appears instead. If it's null, you know the variable isn't being set.
EDIT 1:
As per your message in chat:
You cannot include a remote file like using http://your.domain/connection.php. Well you can but as you saw, it won't work. include("http://your.domain/new/connection.php"); means "execute connection.php as a seperate request and include it's output".
You want:
include(dirname(__FILE__)."/connection.php");

Related

How to know that a command was executed and finished to execute another one(ssh - php )

I'm creating a button on my web page.I want that when someone presses this button an execution of a process of Linux commands on my first server (like "cd file" "./file_to_execute"..) when these commandes are done and finished i want to connect on another server by ssh and to execute another commands.
the probleme is how can i know that the commands before are already finished to proceed to the second part which is to connect on another server .
to resume :
first step connect on the first server , execute some commands
=> when these commands are done ( the part i dont know how to do it )
second step : to connect on another server and execute some others commands.
I'm searching for a way that will allows me to add some pop up to inform the user of my web page that he finished the first step and he started the second.
<?php
$hostname = '192.177.0.252';
$username = 'pepe';
$password = '*****';
$commande = 'cd file && ./file_one.sh';
if (false === $connection_first = ssh2_connect($hostname, 22)) {
echo 'failed<br />';
exit();
}
else {
echo 'Connected<br />';
}
if (false === ssh2_auth_password($connection_first, $username, $password)) {
echo 'failed<br />';
exit();
}
else {
echo 'done !<br />';
}
if (false === $stream = ssh2_exec($connection_first, $commande)) {
echo "error<br />";
}
?>
Thanks
PS: sorry for my English, I'm from Barcelone
To handle events where an exception occurs i would recommend using a try/catch statement, like the one below:
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
When you're trying to handle events and need to know when they finish, there are a few ways to achieve this. You can either set a boolean to true after the command has been executed (like what you are already doing). OR you can return output from the command by printing the output to a txt file and then echoing out the returns of this file. See code below:
exec ('/usr/bin/flush-cache-mage > /tmp/.tmp-mxadmin');
$out = nl2br(file_get_contents('/tmp/.tmp-mxadmin'));
echo $out;
At this point you can create conditions based off of what is returned in the $out variable.

php image upload will not work

I have asked this question before I made changes to my code and my image upload is not working at all I have checked username password, and Root they are all correct. my code will not show any errors I dont know what to do anymore can someone please help me? I have changed my connection for security reasons
<?php
$con = mysqli_connect("localhost", "torcdesi_jone45", "password", "torcdesi_amazing");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query_image = 'INSERT INTO shirt_table (images3)
values( "' . $_FILES['file3']['name'] . '")';
?>
<?php
include("configur.php");
if($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file3"]["error"] > 0) {
// if there is error in file uploading
echo "Return Code: " . $_FILES["file3"]["error"] . "<br />";
} else {
// check if file already exit in "images" folder.
if (file_exists("shirtimgs/" . $_FILES["file3"]["name"])) {
} else {
//move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file3"]["tmp_name"],"shirtimgs/" . $_FILES["file3"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into shirt_table";
if(mysqli_query($link, $query_image)) {
echo "Stored in: " . "shirtimgs/" . $_FILES["file3"]["name"];
} else {
echo'';
}
}
}
}
}
?>
As I stated in comments, your form is missing a proper enctype to handle files.
This I know, since I saw your other question that did not contain it in the form.
<form enctype="multipart/form-data" action="__URL__" method="POST">
As per the manual:
http://php.net/manual/en/features.file-upload.post-method.php

PHP script not able to connect to MS SQL database in webmatrix

I am using webmatrix and trying to connect to a local database in that which is SQL database . I have IIS and SQL Server installed on the computer . The database was created in webmatrix only .
Initially the error display was not on , I managed to do that by manipulating the php.ini file . After that I tried this script which is giving the following result .
<?php
if (function_exists('mssql_connect')){
echo "Okay, fn is there<br>------------------<br>";
} else {
echo "Hmmm .. fn is not even there<br>------------------<br>";
}
if(extension_loaded("mssql")) {
echo "MSSQL is Loaded<br>";
}
else {
echo "MSSQL not loaded<br>";
}
if(extension_loaded("msql")) {
echo "MSQL is Loaded<br>";
}
else {
echo "MSQL not loaded<br>";
}
echo '<br><br>';
$ext = get_loaded_extensions();
if(in_array('mssql', $ext))
echo 'u have mssql installed<br><br>';
else
echo 'u do NOT have mssql installed<br><br>';
?>
The result is :
Hmmm .. fn is not even there
MSSQL not loaded
MSQL not loaded
u do NOT have mssql installed
This is the command I was using to connect to mssql database
<?php
$con=mssql_connect('.\SQLEXPRESS','Shops_localdbU','thePass');
if($con) {
echo "success";
}
?>
and output was:
Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\sample.php on line 3
May be the functions are not installed on your server. If you have access to install them, then you can do that. If not, you cannot use these functions.
If you do have permission to modify the PHP configuration, start here:
http://www.php.net/manual/en/mssql.setup.php

Variable is not printing to the screen when I use echo in PHP

I'm trying to print the host/ip to the screen. But, it's printing: "Resource id #2" instead. I'm using SSH2_connection(). I read the doc page and know the the function parameters are host, port, methods ... but when I try fread($host), the host/ip is still not printing can someone give me some direction on this? Thanks!
Code:
<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($ssh = ssh2_connect('10.5.32.12', 22))) {
echo "fail: unable to establish connection\n";
} else {
if(!ssh2_auth_password($ssh, 'root', '********')) {
echo "fail: unable to authenticate\n";
} else {
echo "Okay: Logged in ... ";
$content = fread($ssh); //Line in question (want ip address to show here)
echo "$content <br>"; //Line in quesion
$stream = ssh2_exec($ssh, 'find / -name *.log -o -name *.txt');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
echo $data; // user
}
}
?>
I believe you need the parenthesis around the variabls as well when using double quotes. "{$content} <br>"
Have you tested with your own debug methods whether the $content variable contains information? You can set a value for the variable to test whether your echo statement is correct syntax.
$content = fread($ssh);
fread() reads from a file and puts the info into a resource handler for use later. I don't think you are using this in the right way currently.
I don't see where $ssh is being defined, but I assume it holds the IP you are wanting to output? If that is the case, just replace
$content = fread($ssh);
With:
echo $ssh;

Cannot modify header information [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I am getting the following error from the following code, and I am not entirely sure why. If you could tell me how to fix it, that would be great. Thanks in advanced.
Warning: Cannot modify header information - headers already sent by (output started at...) on line 45.
<?php
// Initialization
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);
// Error checking
if(!$conn) {
die('Could not connect ' . mysql_error());
}
// Localize the GET variables
$ref = isset($_GET['ref']) ? $_GET['ref'] : "";
// Protect against sql injections
// Insert the score
$retval = mysql_query("INSERT INTO $table(
site
) VALUES (
'$ref'
)",$conn);
if($retval) {
echo "Successfull";
} else {
echo "Unsuccessfull " . mysql_error();
}
mysql_close($conn);
?>
<?php
$url = $_GET['url'];
$loc = 'Location: '. $url;
header($loc);
exit;
?>
Take out the echo calls, you can't send information to the browser before the headers.
You can try something like this to still show if an error happens:
if(!$retval) {
echo "Unsuccessfull " . mysql_error();
}
If you change the headers you cannot output any text prior to to the header command otherwise the headers will already be sent.
ie.
if($retval) {
echo "Successfull";
} else {
echo "Unsuccessfull " . mysql_error();
}
Is outputting text before you change the headers.
Use Output Buffers: http://php.net/manual/en/function.ob-start.php
ob_start();
at the start and
ob_end_flush();
at the end.
What I generally recommend for situations like this, is save all output to the end, as gmadd mentioned, you can do the ob_start, but I prefer to store the data in a string without having to add the extra code (I know you can also designate this in the .htaccess file, I would go that route over adding the actual ob_start items).
What I would do:
$display = ""; // initiate the display string
// etc doe here
if($retval) {
$display .= "Successfull";
} else {
$display .= "Unsuccessfull " . mysql_error();
}
// end of the script right before ?>
echo $display;
?>
The ob_start method works and if you want to go that route, you can add this in the .htaccess file (given that allowoverride is set in your apache setup):
php_value output_buffering On
Again I still recommend the $display storage method, but that is my personal opinion.
Use:
<meta http-equiv="Refresh" content="0;url=http://www.example.com" />

Categories