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.
Related
I have a PHP script of about 10 .php files under the same directory. i.e.
www.domain.com/script_directory/
I wanted to run that script from another director and tried the following:
www.domain.com/some_directory/index.php
where index.php contains
<?php
include "../script_directory/index.php";
?>
Of course the above doesn't work.
Is there any way to set the working path of the script in PHP, similar to "HTML base Tag"?
Some files of the script are "called" via ajax and the others with direct links.
You said:
I want user to always stay at www.domain.com/some_directory
To do it, you can put a HTTP redirect in the index.php at the root, something like this:
<?php
header("location:/some_directory");
If using Apache, you can do it using .htaccess like this:
Redirect / http://www.example.com/some_directory
But all depends on the rules/behaviour that you want. Using include in PHP only insert a script into it, allowing to use the code/functions/classes that are into it.
Just finished doing a simple mail transfer at my site using PhpMailer
I got 3 question about it -
I have read that's needed to store your credentials on a different file, read that there's 2 options - ini/php, which one would be better and how exactly this file should look like.
Regarding the directory of the credentials file, read it should be located outside the web root (just one level above its fine?), in that case how do I call it from inside the web root?
On the same matter, should the Mail.php itself be located on the site directory? or should I take it out as well?
It's generally safest to put values like these in .php files because they will render to nothing, unlike a .ini file which will usually render as plain text.
Yes, one level above is fine - it means that the file does not have a public URL of its own. From a script running inside the web root, you'd just load it with require '../settings.php';
You don't say what Mail.php is, but generally any other PHP scripts can stay put. Things like class definitions are safe because they have no effect when run directly (or at least should have no effect, if you've written them safely!). That said, it's common to put your composer vendor folder outside the web root since you don't necessarily have control over what ends up in there.
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.
I'm trying to use one PHP script on my server that calls other scripts.
The main script, called call.php, is in the public_html folder, so I can send an HTTP request to it using my_website.com/call.php?action=some_script_name&arg1=value&arg2=some_other_value.
I already have a method to form the new request (and execute it), if the action script is in public_html. For example, if some_script.php was located at /public_html/scripts/some_script.php, my HTTP request would be my_website.com/scripts/some_script.php?arg1=value&arg2=some_other_value.
I have that done already, and it works correctly. However, I want to send requests to scripts that are NOT in public_html (or any subdirectory of that). For example, if I have a script under /lib/otherscript.php, I want to call that as well.
I tried a request such as ../lib/otherscript.php?args_here, but that did not work.
Is this possible, and if so, how can I accomplish this?
Edit:
The actual file structure of the (shared) server looks like this (for this example):
/
public_html/
call.php
scripts/
some_script.php
lib/
otherscript.php
You can't access something outside public_html via HTTP; that's the whole point of the public_html directory. You have a few options:
Create a wrapper that is publicly accessible to call the functionality in public_html. The best way to do this is either a class or a function that takes as parameters the arguments from your URL.
Use the command line interpreter.
Either way, you may need to do some user authentication if the functionality is sensitive. If it's harmless, you can put it in public_html. If it's not, you need authentication/authorization checks.
Edited because I misread your question originally.
If you are using cPanel you can access php files outside public_html folder using absolute url, e.g.: <?php require_once('/home/username/lib/otherscript.php'); ?> now you can post parameters to any script within public_html folder at witch you have included otherscript.php.
Note: You have to use your cPanel user name in absolute address.
I am working on a php project and I am having problems with including files.
I have a php script which is located at myapp/reports/index.php. When a form is submitted it performs an ajax post to another phpscript located at myapp/reports/phpHander/submit.php.
Submit.php then has to include a php script which is used to send an email. This is done to ensure that same code can be used over and over again without it needing to be typed for each time it is need. This php script is located at ../../administrator/classes/send.php.
Up to this point is working fine however the send.php script includes another file to get app config settings which is located in administrator/appConfig.php. The problem is this appConfig.php isn't being found even if I put in the full web address of http://localhost/myapp/administrator/appConfig.php.
What am I doing wrong. I am using the php include function to do this and its working for everything else but it seems to have a problem then including another script from a different location.
Thanks for any help you can provide.
http://localhost/myapp/administrator/appConfig.php is only URL path.
You need absolute filepath like C:/wamp/www/myapp/administrator/appConfig.php (Windows) or /var/www/myapp/administrator/appConfig.php (Linux)
Anyways best way is make a file "dirs.php" in your root application directory with constant:
define('ROOT_DIR', dirname(__FILE__));
where dirname(__FILE__) will be absolute path to your app directory.
With this knowledge you can include files in this way:
myapp/reports/index.php:
require_once('../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
myapp/reports/phpHander/submit.php.:
require_once('../../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
When you include another PHP script, all the paths are relative to the calling script. So, it sounds as though your script is at myapp/reports/phpHander/submit.php and includes ../../administrator/classes/send.php, which then includes another script in that same directory. In this case, you need to use the path "../../administrator/appConfig.php". Alternatively, you could use absolute paths relative to the filesystem's root.