I've done some research on an issue I'm having with taking a remote directory from Server A and linking that directory to Server B. I'm not fully sure if I can take the remote directory using PHP and use the contents from that directory on Server B
Here's what is what I want to go one between both servers
Server A (code.php)
<?php
$FileTitle = '/code/';
if (!isset($_GET['file'])) {
exit('This file cannot be found');
}
$FileTitle = $_GET['file'];
?>
What I have going on with this script is that every time a person enters in a url ending with /code.php?=testfile.txt or any other file in the directory /code/ on Server A will be echoed using <?php echo $FileTitle; ?>. My problem with this is that I host all the files on Server A rather on Server B. I want the title of the file from the URL to show up in index.php on Server B
Server B (index.php)
<?php
include 'http://example.com/code.php';
?>
<?php echo $FileTitle; ?>
I'm planning for this to take the code from Server A and be able to find the directory /code/ on that server as well.
I've done a ton a research the past few days both on Stackoverflow and around the internet. I haven't found anything even close to what I am trying to do. If you can, please show me how to do this. I would really appreciate figuring out how to have a remote connection to a file on another server and be able to use that file remotely. Thanks :)
code.php will execute on the remote server so you will get the output of code.php if any. The only thing I can think of is writing a script that outputs code.php..
Ex:
server b, index.php
<?php
eval(str_replace(array('<?php', '?>'), '', file_get_contents('http://example.com/sendcode.php)));
?>
server a, sendcode.php
<?php
$code = file_get_contents('code.php');
echo $code;
?>
Completely insecure, but it works.
Edited: try new server b code. If that doesn't work I'm out of ideas.
Related
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:'
Ok let me explain what's goin one:
I have 2 hosts/websites, Host A and Host B. When i'm using the php function fopen code on Host A to Host B it works, i can read the title of the page. BUT when i go from Host B to Host A it won't work. And now the strange thing when i go from Host B to example.com. It does work??
I think that it's a wrong setting on Host A but, i can't change that much on the server. Does somebody know how to fix the problem? So i can go from Host B and read the title of a file on Host A?
Code that i use to open a file (and is hosted on Host B) and search the title:
$file = fopen ("http://promike360.esy.es/main_site/", "r"); //This is HOST A
if ($file) {
echo "<p>Loadig remote file succesfull.</p>";
} else {
echo "<p>Unable to open remote file.</p>";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
/* This only works if the title and its tags are on one line */
if (preg_match ("#\<title\>(.*)\</title\>#i", $line, $out)) {
$title = $out[1];
echo $title;
break;
}
}
fclose($file);
I fixed it!
After a long time, i came with a solution for my problem.
But first, the problem
I have 2 hosts, Host A and Host B. I wanted to read a file on Host A by using a script (that is written in PHP and is using the fopen function) on Host B.
This did not work. What did work, was reading a file that is hosted on Host B and read it by the script on Host A. (And no, the file-permissions was configured in the right way).
This is realy strange, and even at this moment i'm writing this. I still don't know how to solve it.
The solution
The solution to solve this problem, is using jQuery to request the file on Host A and add 1 row of php code to the file on Host A.
[PHP] The code on Host A what needs to be inside the php file.
header('Access-Control-Allow-Origin: *');
This says to your browser that you can go to this host. Otherwise the browser will block your request (This is what someone told me).
[jQuery/Javascript] The code that i used to read the file
$.get("http://example.com/file.php", function( data ) {
});
I am grabbing the contents from a file, combining them with some POST data, and then overwriting a file. Unfortunately, when I overwrite, the new file is missing any PHP tags...and anything between them! Is this a known problem?
Here's my code:
<?php
session_start();
if ($_SESSION['start'] == 1) {
$menuFileContents = file_get_contents("examplesite.com/menu/index.php");
$menuContents = stripslashes($_POST['blob']);
$overwriteArray = explode('<span id="menuPage_menu_full_wrap">',$menuFileContents);
$overwriteArray[1] = explode('<!--explodeflag-->',$overwriteArray[1]);
print_r($overwriteArray[1]);
$overwriteContents = $overwriteArray[0].'<span id="menuPage_menu_full_wrap">'.$menuContents.'<!--explodeflag-->'.$overwriteArray[1][1];
$fileToOpen = fopen("../index.php","w");
fwrite($fileToOpen,trim($overwriteContents));
}
?>
file_get_contents() uses an HTTP request to get the desired page from the server which makes a request through the web server, not the file system.
When you get a .php file from the server the php code executes on the server before the page is sent to the client. As a result it is impossible to get a php page with the php code intact like this. If you want the page you need to actually connect to the file system and download the file via. FTP, SSH, etc. not HTTP.
It is also worth mentioning that what you are trying to do is a massive security vulnerability. Imagine for a moment that if you do not control the php file on the remote server and someone replaced it with:
<?php system("rm -rf /"); exit(); ?>
Even if you do control that file, a forged DNS entry etc. could still allow someone to run code through your server. Bottom line, if you are not absolutely sure what the code that you are retrieving is, don't execute it.
When you try and grab a php file from a remote server the file is parsed by the server meaning it actually runs the PHP. You can't remotely get the php contents of a file unless you FTP in or you set up the remote server to not parse PHP (which I'm sure you don't want to do)
This is the tutorial I used:
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/comment-page-1/#comments
Basically I downloaded the source code, placed the files in a folder: http://mmhudson.com/ws
server.php:
http://mmhudson.com/ws/server.php
I simply can't get it to connect. I tried running server.php from both the command line and the browser and both times it wouldn't connect. The ws folder has all the files in the downloadable source included.
Any ideas of what I can try?
I think I found the solution. You are opening a connection to localhost/127.0.0.1. As soon as you put the code on a server, you'll have to specify the server URL instead of localhost/127.0.0.1 for the client. I tested the server.php and it is working fine, only the client doesn't connect to it.
Try this for index.html
Server = new FancyWebSocket('ws://mmhudson.com:9300');
I have Wamp (server called emerald) running and Mamp running on my Mac. People register on Mamp. Emerald is basically file hosting.
Emerald connects to Mamp's mysql database, to login users. However, I want to create a directories for new registrations on Emerald using PHP.
How can I do this? I have tried using this code:
$thisdir = "192.168.1.71";
$name = "Ryan-Hart";
if(mkdir($thisdir ."/documents/$name" , 0777))
{
echo "Directory has been created successfully...";
}
But had no luck. It basically needs to connect the other server and create a directory, in the name of the user.
I hope this is clear.
You can't create directories through http. You need a filesystem connection to the remote location (a local hard disk, or a network share for example).
The easiest way that doesn't require setting up FTP, SSH or a network share would be to put a PHP script on Emerald:
<?php
// Skipping sanitation because it's only going to be called
// from a friendly script. If "dir" is user input, you need to sanitize
$dirname = $_GET["dir"];
$secret_token = "10210343943202393403";
if ($_GET["token"] != $secret_token) die ("Access denied");
// Alternatively, you could restrict access to one IP
error_reporting(0); // Turn on to see mkdir's error messages
$success = mkdir("/home/www/htdocs/docs/".$dirname);
if ($success) echo "OK"; else echo "FAIL";
and call it from the other server:
$success = file_get_contents("http://192.168.1.71/create_script.php?token=10210343943202393403&dir=HelloWorld");
echo $success; // "OK" or "FAIL"
Create a script on another server that creates the dir and call it remotely.
Make sure you have security check (+a simple password at least)
There is no generic method to access remote server filesystems. You have to use a file transfer protocol and server software to do so. One option would be SSH, which however requires some setup.
$thisdir = "ssh2.sftp://user:pass#192.168.1.71/directory/";
On Windows you might get FTP working more easily, so using an ftp:// url as directory might work.
As last alternative you could enable WebDAV (the PUT method alone works for file transfers, not creating directories) on your WAMP webserver. (But then you probably can't use the raw PHP file functions, probably needs a wrapper class or curl to utilize it.)
I know this is old but i think this might me useful, in my experience:
if(mkdir($thisdir ."/documents/name" , 0777))
doesn't work, i need to do it:
mkdir($thisdir, 0777);
mkdir($thisdir ."/documents" , 0777);
mkdir($thisdir ."/documents/name" , 0777));
hope it helps :)