I wish I knew how to make a file that is a principal access to a web page. I'm using PHP to do this. It occurred to me the following:
function crearArchivoUrl($url){
$archivo=str_replace(array("http://", "https://", "mailto://", "ftp://"), "", $url);
$archivo=str_replace(array("/"), "-", $archivo);
$this->checkNombreDestino($archivo);
$contenido="[InternetShortcut]\r\nURL=".$url."\r\n";
$fp = fopen($archivo, 'w');
chmod($archivo, 0644);
fwrite($fp, $contenido);
fclose($fp);
}
But when I test it (by double-clicking on it) I did not jump the browser.
Anybody can tell me how to make files that are shortcuts to web pages?
Thanks for the help.
Greetings!
My code was correct, all I needed was that the file extension should be .url, ie, my-web-shortcut.url
Thanks for everything.
Greetings!
Your PHP will only be executed if you have a PHP interpreter installed, and if the webserver is configured to run your PHP file. If you distribute this file to unconfigured computers, it will never be executed as PHP code, and will therefore never run as you expect.
If you are running this script on a webserver that is properly configured, consider using header("Location: <URL>") to redirect the user to the new page.
Actually, you are reading a file, and writing it to another file. Doing that sends nothing to the web browser.
A first approach would be to send a header Location to redirect the broswer. See examples in the manual to know how to use it.
Another approach would be to read the file contents, and to print it, using echo or any other printing command.
Related
I am running a file that reads a large json file and then saves the content to database.
When I am running the file directly, it saves the data
But when I am trying to run it via cron job, it is saving null into database.
Here is my code
include('conn.php');
$file = "https://api.binance.com/api/v1/ticker/allPrices";
$json = file_get_contents($file);
updatePrice();
function updatePrice(){
global $con;
global $price;
$sql="INSERT INTO table(price) VALUES('$price')";
if(mysqli_query($con,$sql)){
echo "price updated";
}}?>
Thanks in advance!
Ensure that the value of location of the file is absolute rather than relative path. Cron job is using another id (usually root) so you need to explicitly use /home/userid/path/to/file.txt rather than ./file.txt.
Hope this helps. Thanks.
Edited: This also applies to your function file_get_contents.
Enable error reporting on your script and take a look for warnings or errors.
Check if allow_url_fopen is enabled for cli (some systems have separate .ini files for cli and web). You can do php --info to check which file is used on the cli.
I strongly recommend not using file_get_contents(), because it doesn't handles redirects or delays in response. Try cURL instead: http://www.php.net/manual/en/ref.curl.php
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)
I am trying to include remote php files as a resource but I am having a bit of trouble. I went into the php.ini files and set allow_url_fopen to ON. I also looked for the setting allow_url_include but it was not in the file, I added it to the php.ini file and also set that to on.
If I try to include using
include ('http://somewebsite.com/lib/somescript.php');
The server / php spits out a message saying:
URL file-access is disabled in the server configuration
I also get a message saying:
failed to open stream: no suitable wrapper could be found in blah blah blah
The seconed way I am trying to acomplish the same result is using fopen but I am just getting the content of the file, thats not what I need I need my local script to see the remote script as an executabel rescource.
$myscript = fopen("http://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
// include in the contents of my_script.php
echo $incmyscript;
I have to be doing something wrong? I know echoing out the variabel $incmyscript is wrong, but I can't think of a way to place in the code. I am not sure if fopen is the best best way to get what I want.
Any ideas?
The message you are getting:
URL file-access is disabled in the server configuration
Indicates that the allow_url_include setting in your php.ini is set to Off. Enabling that option will allow you to do remote file inclusion, but be very careful with this as it's a pretty big security risk once the other site would be compromised (A hacker could easily inject their own remote code to your site).
Instead of echo, you could use eval.
Only do this if you want to execute PHP code from the other server, not if you just want to include HTML!
Even if you really want to execute PHP code from the other server, a man-in-the-middle could execute arbitrary PHP code on your server. You should therefore better use HTTPS or avoid the inclusion of the remote file at all.
Example:
$myscript = fopen("https://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
$incmyscript);
Instead of the echo you could use this:
eval($incmyscript);
But be careful, this is very bad practice!
READ THIS: http://php.net/manual/en/function.eval.php
If you can trust remote script then you can call eval:
eval ($incmyscript);
If http://somewebsite.com/lib/somescript.php served by server supporting PHP you're trying to include it's output, not the code itself! Otherwise it's a just wrong and may be considered as security hole!
What you're trying to do is opening of a major security hole!
If the remote server is configured to process .php files, you won't be able to get the source for it. The server will process the PHP and then return any output. If getting remote PHP sources were possible, hackers would be grabbing our code and looking for vulnerabilities way too easily!
I'm having a critical issue where my WAMP installation for PHP 5.3.0 is not finding a file which exists within my computer. Does anyone know anything about this? Possibly a PHP Bug?
Any help would be much appreciated.
Here is the variable which creates the file:
$baseNewsUrl = "C:/reviews/reviews/$platform/$fullname";
And here is the code which grabs the contents:
if(is_file($baseNewsUrl)){
$contents = file_get_contents($baseNewsUrl);
} else {
echo "File not found. " . "\r\n";
continue;
}
Here is the output of $baseNewsUrl: C:/reviews/reviews/GBA/r20107_GBA.htm
And the file does exist.
Check that the entire path leading up to your file is readable by the user PHP is running as (if you are using IIS, this might be something like "Network Service," although I am not particularly experienced with PHP on Windows). Also, check whether the INI directives "open_basedir" or perhaps "safe_mode" are set--these would give PHP self-imposed limits on which files are accessible.
Do a var_dump (not an echo) on your variable.
var_dump($baseNewsUrl);
and look at the actual contents. You may have some invisible garbage characters in there that's preventing Windows if you're doing this in a browser to make sure there's no empty tags (or other browser-render-invisible) characters.
If that doesn't reveal anything, remove the is_file check and try to open the file with file_get_contents (or any file related function) and var_dump it's contents. You'll either open the file, or PHP will spit out an error/warning/notice (either to your browser or to your error log) that should let you know why it can't open the file.
I'm gonna say this, and it very well might not be your problem but it is a recurring one for me. If you use skype on your computer, it has a somewhat known compatibility issue with WAMP. It cause's WAMP to be unstable, not load files properly.. everything.
on windows
$baseNewsUrl = "C:\\reviews\\reviews\\$platform\\$fullname";
It's due to Windows Vista and WAMP.
i want to open a notepad from php file and the notepad should contain the text which i declare as string in php file. essentially a notepad should open with the text I pass from php file.
If the PHP file is executing on a web server you cannot cause a web browser to open a new process like that. I'm sure you can imagine what a security hole that would be!
If you're running a PHP file as a local script in CLI mode, you should be able to launch notepad like any other process, e.g. using backticks or exec etc.
However, if you really wanted to do this server side, the best you could do is have a PHP script which used a Content-Disposition header, e.g.
//tell client we're delivering text
header('Content-type: text/plain');
//hint that it's a downloadable file
header('Content-Disposition: attachment; filename="textfile.txt"');
//output our text
echo "The quick brown\nfox jumps over\nthe lazy dog.";
The user can then save this file and open in their editor of choice.
You can't get PHP to open a window on the user's machine, because PHP is run entirely on the server. By the time the output reaches the browser the script will generally have terminated - you can only do what you can ask the browser to do and what it will let you (using HTML / headers etc.). For security purposes the browser will not (or should not) let an arbitrary website do very much with your machine - e.g. it will not let you spawn new Windows processes.
The best I think you could do is something like this:
$string = 'a string';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="file.txt"');
echo $string;
This will send the relevant headers so that the browser will treat the content as a download called file.txt of type plain text. The browser should prompt them to download a file which will be likely to open in notepad, unless they have changed the file association for .txt .
However you won't be able to get any changes back that the user makes to the document unless you ask them to upload it, so I'm not sure this is a good solution to what you are trying to acheive.
It is possible to execute a program from php but only server-side.
So imagine the server runs Windows, it would start notepad server-side.
PHP gets executed on the server, and has nothing todo what's running client-side.
Technically, to do this you'd have to create a file and then execute a system with that file as a parameter. Something like this:
//String to show in notepad
$myStringToDisplay = "some text to show in notepad";
//Write this string to a file
$myFile = "somefile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $myStringToDisplay);
fclose($fh);
//Execute notepad with this file as a parameter
system("notepad.exe ".$myFile);
However, this is going to execute Notepad on the server running the PHP file (if system calls are even enabled on your server), which is probably not what you want to do. PHP cannot execute any code on the client machine, and certainly cannot make a system call to execute any program it wants on the client (thank god). It would be a huge, huge security breach.
If this does not accomplish the desired functionality, please tell us what you're trying to do and why. This does not sound like a very sensible request.
It is not possible to open a program from your php application. But you ca load the text file using a PHP text editor. You will also be able to load the values which you are talking about.
http://www.fckeditor.net/ is one such editor.
First you can "create" the file and fill it with text.
Execute shell command: echo $text >> $filename
then execute: notepad $filenameToOpen
Thats it.
To open a notepad from php script we will use command line inter phase.
Firstly we will create one php file in that we will write:
var_dump(popen('notepad','r'));
Then we will save that with some name like notepad.php
then open command prompat there we will give the path of our file to run our file
like:
d:/>wamp>www>php notepad.php
It will run our php file and it will oopen notpad.