PHP mysql connect to any remove server the post variables - php

if (!empty($_POST)) {
$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$db = $_POST['db'];
echo '<pre>';
print_r($_POST);
$con = #mysqli_connect($host . ":3360", $user, $pass, $db);
// $con=#mysqli_connect('192.168.100.42','root','vertrigo','skates');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo 'connected';
}
#mysqli_close($con);
}
I'm using this script to access remote db server in local(basically i want to access local db server through live webiste), howewer, I'm getting an error:
Failed to connect to MySQL: Unknown MySQL server host
'192.168.100.42:3360' (11004)
Please help to get out from this.

192.168.100.42 looks like a local IP. You'd need to try to connect to your static public IP and then forward port 3306 in your router to the 192.168.100.42 machine.
That's not your only problem;
Just because $_POST is not empty, don't assume it has those array keys set and not empty. Syntax like
if(isset($_POST['user'] && !empty($_POST['user'])))
Is much better.
Also, never use $_POST on any database interaction without sanitisation, and never connect to a database with the root user.
I don't know your exact application, but it's impossibly insecure.

Related

Filezilla Connect through PHP

I am trying to upload a file from my machine (client side) to the FileZilla server (server side) I have for storing the web page files.
When trying to connect to FileZilla through PHP I receive the following error message:
Connection failed: Connection refused
Usually, I would expect the error when the login credentials are incorrect, however, in this case, they are correct.
My question: Can you connect to FileZilla via PHP? I am sure the answer must be 'Yes' however due to the technical difficulties currently I would not be surprised otherwise.
Potentially there is an error in the formatting of the connection function.
var $host = "xx.xx.xx.xx";
var $user = "xx";
var $pass = "xx";
var $connect;
function serverConnection()
{
$conection = mysqli_connect($this->host, $this->user, $this->pass) or die
("Connection failed: " . mysqli_connect_error());
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$this->connect = $conection;
}
return $this->connect;
}
The goal is to be able to upload files from a form to FileZilla, to act as users profile pictures.
Currently, you're using mysqli_connect which connects to a database server, you need to connect to an FTP server, so you should use the FTP functions. You should first connect and then login.
It should look something like this:
$server = 'myServer';
$user = 'myUsername';
$pass = 'myPassword';
$connect = ftp_connect($server) or die('Could not connect to FTP-server.');
if(ftp_login($connect, $user, $pass)) {
echo 'Connected to FTP-server.';
}

c9.io php pdo connect to mysql

I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');

Connecting to a remote mysql server

How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

mysql_connect always allows access

I am attempting to connect to a mySQL database running on my local computer using a local test server set up using XAMPP. I am just using the root account which has no password assigned. I tried it as below and everything appears to work fine. I got my message echoed out to say I had successfully connected. I then tried to force an error by entering anything else in as the host, username or password and it still echoes "connection successful".
This is the code I am using to connect:
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$myConn = mysql_connect($db_host, $db_username, $db_password) or die('Connection error: '.mysql_error());
echo 'connection successful';
?>
I was wondering whether it is me or maybe the install of apache/mySQL that I am using that is causing such an absurdity.
Something must be screwed up with your install:
marc#panic:~$ php -a
php > $x = mysql_connect('bleargh', 'foo', 'bar') or die(mysql_error());
PHP Warning: mysql_connect(): Unknown MySQL server host 'bleargh' (2) in php shell code on line 1
Unknown MySQL server host 'bleargh' (2)marc#panic:~$
About all I can think of is you've got a DNS server that returns a valid address for ALL lookups, causing all hostnames to resolve.
try this one
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$con = mysql_connect($db_host,$db_username,$db_passwd);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
echo 'connection successful';
mysql_close($con);
?>
It will always echo 'connection successful' because you aren't checking the status of the connection.
if (!$myConn) {
echo 'Sorry, there was an error '.mysql_error();
} else {
echo 'connection successful';
}
Depending on how you executed the code, you might be running into this:
If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned. The new_link parameter
modifies this behavior and makes mysql_connect() always open a new
link, even if mysql_connect() was called before with the same
parameters. In SQL safe mode, this parameter is ignored.
So, either do an explicit check or:
$myConn = mysql_connect($db_host, $db_username, $db_password, true) or die('Connection error: '.mysql_error());

Database code is not connecting in chat program using php

<?php
// MySQL database connection file
$SERVER = "127.0.0.1"; // MySQL SERVER
$USER = "root"; // MySQL USER
$PASSWORD = "admin"; // MySQL PASSWORD
$link = #mysql_connect($SERVER,$USER,$PASSWORD);
$db = mysql_select_db("website");
?>
This is a database connecting code for chat program but it not connecting,Can any one pls help me to correct this code?
Drop the # infront of mysql_connect, it's used to suppress error which you don't want.
Also you need to check the return value of mysql_connect which is there in $link and make sure that it is not false before you proceed and to a DB select. Calling the function mysql_error when an error occurs gives you the reason for the error.
$link = mysql_connect($SERVER,$USER,$PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

Categories