PHP MySQL fails when page is viewed remotely - php

I have two servers on my local network - one a web frontend and the other a MySQL backend. I have a PHP script that looks like this:
<?php
error_reporting(-1);
echo "Connecting...\n";
$link = mysql_connect("192.168.1.15", "-----", "-----") or die(mysql_error());
echo "Communicating with the server...";
mysql_query("INSERT INTO .....
//More code down here...
?>
This script is called on my web frontend to connect to the backend server. When this script is accessed from the local network (i.e. when I open the page by going to http://192.168.1.14), the script outputs
Connecting...
Communicating with the server...
and a row is added to the database, as it should. However when I connect remotely (i.e. going to http://myDomainName.com/mysql_insert_script.php) from a connection not on the local network, all I see is:
Connecting...
No error messages follow, the script just cuts off, and no data is added to the database. When I place a second, 'proxy' script on the server that simply requires() the above script and then I access the proxy remotely, everything works fine. Below is the proxy script, so you can get a better idea of what works and what does not:
<?php
//this script makes it appear that the mysql_script is being viewed from the local network
//I exist on the web frontend at 192.168.1.14
require("http://192.168.1.15/mysql_insert_script.php");
?>
I am sorry if I can't provide any more information, but I am stumped. Any help would be appreciated.
Chris
P.S. - I have verified that the mysql server is accessible from external hosts on the local network, but I have a firewall that prevents connections from outside my network. I don't think this would matter, however, as the MySQL server and the PHP script connecting to it are both run on the local net.

you got wrong server name

Related

Hard time to connect to my database with php

I am trying to build a homne server for studies on a raspberry pi 3.
I am just trying to connect to my raspberry database and php base to use my database on a HTML page.
I tried everything 9not apparently), to change several times my login, i tried with 'root' and create an other user and give all privileges etc, but nothing seem to work. I have all installed, php work just fine, i can reach my server with a domain name hosted by noip. the html and php content pop just fine.
I can even connect to my database via ssh.
But since few month i tried to make it work and i am desesperate, so if someone can help me ...
<?php
$db = mysqli_connect("mywebsite","root","myLogin","mydbName")
or die(mysqli_error($db));
?>
so i would just be able to make request on my database, i am just blocked on that step
$con=new mysqli("localhost","root","root_password","database_name");
Generally, root has no password so the connection should be like this
$con=new mysqli("localhost","root","","database_name");

ESP8266 to XAMPP server

Recently I have been trying to work with my ESP. I have set up a server using XAMPP on my laptop. I can access that server using 127.0.0.2. I know that the content viewed on this page is present in the htdocs folder in XAMPP.
So I have created a folder named TEST in htdocs. The ESP is connected to PSoC and is sending some data at regular intervals to the TEST folder on the server. My question is: what is the IP address that will be needed to connect to the server when I use the AT+CIPSTART command on the ESP side?
Is it 127.0.0.2 or some other IP from the router? I have tried sending data to ThingSpeak before and there they provide a ready-made GET request link to send data to the server. What will be the GET request link in my case if the server is created using XAMPP? Please help as I am new to networking. Thank you.
Ok. I wrote a php file which takes value in a variable SAP and writes into a text file which is stored in the ht docs folder. The php code is as follows
'
$content = "SAP ID :".$var1." is present for the lecture \r\n";
echo $content;
echo "<br >";
$status = file_put_contents('attendance_record.txt',$content,FILE_APPEND);
if ($status != false)
{
echo "Data is written to the file :p ";
}
else
{
echo "Data was not written into the file :( ";
}
?>'
After then I enable the Xampp server and access this file by using 'http://127.0.0.2/receiver.php?SAP=104' Until this point everything is working fine and the text file is being updated. Now the same thing is needed to be done via AT commands using the ESP. The following lines of code is running on the ESP side.
AT+CWJAP="SSID","PASSWORD"
AT+CIPSTART="TCP","192.168.0.104",80
AT+CIPSEND=35
GET /receiver.php?SAP=69 HTTP/1.1
Now i'm not getting any updates on the text file. Any help will be appreciated. Thank you.
172.0.0.2 is the same at calling 'localhost' so you would need your laptop's IP for the ESP to connect to. Depending on your OS you can find that opening your terminal and write ipconfig(windows) or ifconfig(linux) On a MAC go to System Preferences > Network and your IP is displayed under'Status:'

Is it possible to go from localhost to external adress

I can connect to my localhost and get info from a local phpMyAdmin database.
Now I want from a php file, that is stored on my local computer, but now the php file refers me to go to an external website:
<?php
//mysql_connect('localhost','root','root');
mysql_connect("http://eternal website","username","password");
mysql_select_db("dlvplant_portret_v1");
$q=mysql_query("SELECT * FROM people WHERE voornaam LIKE '%%'");
//$q=mysql_query("SELECT * FROM people WHERE voornaam ='""'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
I tried the link and copy/paste the username and pass and I use the good website link. (the link brings me at the myPhpAdmin login from the server). So everything works and is filled in good. But when I try to connect with my android phone (from phone to local comp's php file) then the php file refers me to go to external webpage, but it says: Host is unknown
Is it even possible to go from local to external and back again?
You can connect to external MySQL hosts - but you do not use the HTTP protcol for it. And external access needs to be enabled on the MySQL server.
You should use plain ip address(xxx.xxx.xxx.xxx) or single "DOMAIN.SOMETHING" instead of "http://DOMAIN.SOMETHING"
mysql_connect("IPADDRESS","username","password");

HTTP Get with variables

I have spent 2 days on this and are now stumped, having tried dozens of different methods and lots and lots of googling.
I need to retrieve data generated by a php page running on apache, using the GET command. This works fine when I am connecting with the LAN, but if I connect remotely then I am unable to get any response when there are variables in the path.
This works bot locally and remotely and I get the expected output from the php page.
GET /myfolder/mypage.php<CR><LF>
However, add a variable to the path and I get the correct response on the LAN but connecting remotely I get no response whatsoever:
GET /myfolder/mypage.php?var1=1&var2=2<CR><LF>
Just to reiterate, this works fine when accessing from the LAN and works fine in a web browser, but when I send as effectively a Telnet request I get nothing at all.
Any ideas ??
EDIT
The request is being sent to Linux Apache web server via a serial > tcpip gateway with a carriage return and linefeed at the end. The syntax works perfectly on the LAN with the ?var1= etc but as soon as I add ?var=1 when connecting externally nothing at all is returned, in fact apache logs don't show any record of the connection request.
I ## SUSPECT ## this may be something related to apache mod rewrite ?
May be can try something like below and specify the host and also the ver of HTTP being used.
GET /myfolder/mypage.php?var1=1&var2=2 HTTP/1.1
Host: 127.0.0.1

Why would ftp_connect() return false on production server if it works elsewhere for connecting to the same FTP server?

I have a script that uses ftp_connect() among other FTP PHP functions for uploading a file.
ftp_connect() works when executed on my local development server for connecting to a remote FTP server. The same script, when executed on the remote server does not work for connecting to the exact same FTP server.
Could somebody please point me in the right direction?
Thanks!
Here is the code:
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$server = 'ftp.someserver.com';
$ftpConn = ftp_connect($server);
if(!$ftpConn)
echo 'failed';
else
echo 'success';
No errors are reported.
So if I understand it correctly then the script above is installed on the server that you're trying to access using FTP (ie. the script is opening a local FTP connection)? What's the use? FTP in PHP is only useful to transfer files between 2 servers, you cannot use it to transfer files from the client to the server (since the script is executed on the server).
edit
Something I didn't add in my original comment : you could use a Java FTP applet if you want to transfer files from the client to the server. But be aware of the security issues involved (because the user credentials can be sniffed :p).
Probably firewall issues. On top of that, FTP was not designed with NAT in mind.
Try to login to the production server and use a ftp client to do the same connection.
I do not know the things inside it very well but I want to give my little help.My server is ubuntu Linux with Apache、PHP and MySQL,and my develop env is MAMP on Mac.
I met the problem suddenly and cannot find what happened because it was worked yesterday,I searched many answers and can't solved it.The ftp_connect($ftp_server) only return bool(false),but I can use my FileZilla,interesting,is it?
So I try to connect the server from my command line,like ftp 111.22.333.44,It shows:
500 OOPS: cannot read user list file:/etc/vsftpd/vsftpd.user_list
I login in my ubuntu server, and didn't find the vsftpd directory,and the vsftpd.user_listis in the directory /etc/,still don't know what happened.
So I simply create the directory and copy the file vsftpd.user_list to it.Then I try ftp 111.22.333.44(your IP address) again and it works now.
hope it help someone else.

Categories