php script to access multiple web pages - php

I need a function/method in php to access multiple web pages in a loop. Just as someone can manually access a web page and load any scripts on there. I'm not downloading any information I just want the script to access it so that any php code can run on that page. It's a hack for a program i'm working on that needs cron jobs running. The cron job will run one script that will load multiple pages eg. http:// localhost/program/script1, http:// localhost/program/script2. I can then dynamically add pages from a database as time goes on.

here you would just separate the code you want shared into another file and then use
require("/path/to/filename.php");
The path instead of being a url will be the filesystem path to where you saved the file.
Good starting points to reference this file is $_SERVER["DOCUMENT_ROOT"] so you could say something like.
require($_SERVER["DOCUMENT_ROOT"]."/program/script1.php");

You could scan a directory and loop through each of the files in it then read or write to it with fopen
$files = scandir('folder/');
foreach($files as $file) {
//do your work here
$fhandle = fopen($file, 'r');
}
http://www.w3schools.com/php/func_directory_scandir.asp
http://php.net/manual/en/function.fopen.php

Related

PHP opendir() not opening apache alias directory [duplicate]

I'm new to PHP, and I'm trying to build a script. When I load the script, I get the following error:
Warning: opendir(http://www.hetweerinboskamp.nl/voorpagina/movies) [function.opendir]: failed to open dir: not implemented
<?php
$hal ='';
$dir ='http://www.hetweerinboskamp.nl/voorpagina/movies';
if ($handle = opendir($dir)) {
// Loop the folders
while (false !== ($file = readdir($handle))) {
if(strlen($file) > 4) {
$rawd = parsename($file);
$hal.= 'new Date('.substr($rawd,0,4).', '.substr($rawd,4,2).'-1, '.substr($rawd,6,2).'),';
//$hal.= $rawd.',';
}
closedir($handle);
}
opendir() is used to open a local directory and since PHP 5.0.0 on an ftp directory.
If your PHP code runs on www.hetweerinboskamp.nl then /voorpagina/movies is actually a local directory and you can do this:
$dir ='<wwwroot>/voorpagina/movies';
if ($handle = opendir($dir)) {
where wwwroot is the root of the filesystem as seen by your php code.
If you're trying to download content from another website, try e.g. file_get_contents(). Note that if the remote server lists the content of a directory the listing is in fact an HTML page generated on the fly by the server. You may find yourself needing to parse that page. A better approach is to check whether the server offers some sort of API where it sends back the content in a standardized form, e.g. in JSON format.
opendir operates on directories on a filesystem, not HTTP URIs.
While some HTTP URIs return directory listings (the one you are using doesn't, it is a 404 error), those listings as HTML documents generated by the webserver and are not actual directories.
Manual claims this function works with URL's, however, it appears it doesn't.
Use a local path (either relative or absolute). For example, './voorpagina/movies'. This has solved a similar problem to me before. I hope this helps.
Most remote servers does not send a directory listing back as such opendir cannot understand what your trying to do so it cant work.
You will need to use something like ftp, here is an example: http://php.net/manual/en/ftp.examples-basic.php or cURL

PHP directly accessing the required files

So I work on a website and to make things easier I made specific files for every task (like: for the top menu I made menu.php) and then require(); them in the main files. All is good but I tried accessing in the browser /include/menu.php and it shows up. I don't want people to access them whenever they want, I just want to require them and to be available only through the main file.
The easiest way to prevent other php files from being accessed, is to define a variable in the main script:
define('IN_APPLICATION', true);
In all of your other files, simply add:
if ( !defined('IN_APPLICATION') )
die('You cannot access this file directly.');
An alternative way is to use an .htaccess file. If your server is running apache, this is all you will need. Simply put this file in your /includes directory.

PHP file handling and third level domains

I've a subdomain like software.domain.com that is a redirect to domain.com/software/ through cPanel redirect.
I'm creating an upload system that send all the files in a sibling folder, like domain.com/files/, no problem uploading with some tricks but when it's time to delete that file, that specific script cannot find it...
Returning the $_POST variable send to the script, the path is right, as /files/path/to/file, it refers to the public_html root as it has to be!
The script is invoked using an ajax call, it could be it? Maybe some path translation...
Thanks a lot!
If you are saying your AJAX can't find it from the subdomain, or anywhere else, you need to use the full domain path, WITHOUT the subdomain.
var url = "http://domain.com/files/path/to/file";
If you're saying your PHP can't find it, you can do something like this to ge the absolute path. From a file in the root of your SUBDOMAIN...
$url = realpath(dirname(dirname(__FILE__)))."/files/path/to/file.php";

PHP MySql Request from outside public_html (Cron Job)

I want to use cron job to make a script that sends scheduled emails to my clients. For more security I will put this PHP file outside public_html folder. I want this file to be a part of my original script So I need to include (mysql_connect.php) in this file so I don't need to connect to database in this file. Is this technically possible?. and How can I call this (mysql_connect.php) file that includes the connection and all my functions that run my original script. Thanks
It should not really make any difference if you're calling it from within public_html or not. You can include the file in the same way. For example say you're setup like this:
/home/username/myscript.php and /home/username/public_html/mysql_connect.php
Your myscript.php file might look something like this:
<?
include_once('/home/username/public_html/mysql_connect.php');
// Your script
?>
There are better ways to include files than this, e.g. using $_SERVER['DOCUMENT_ROOT'] but there are plenty of other posts on that.

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

Categories