0
Quick Solution (thanks to Tom's Answer below, read his for full explanation): Create a new php file in the webroot directory; let's say temp.php. From there, use include or request to call the test.php file that's outside the webroot.
index.html -> temp.php -> test.php
- I'm using javascript to go to temp.php, but href can be used too.
- In temp.php, I used: include dirname(__FILE__). '/../test.php';
- And that's it, test.php can't be accessed directly by URL. If you want to go to a file inside webroot from outside, use something like: header("location:".dirname(__FILE__).'/../wwwroot/index.php'); See: http://stackoverflow.com/questions/13550471/php-include-file-in-webroot-from-file-outside-webroot, Where I found FILE/DIR stuffs

Original Problem:
I have a website hosted on Arvixe (Windows Hosting, not Linux).
In my domain directory, I have a test.php file and the wwwroot folder.
Inside the wwwroot folder, I have an index.html file.

From the index.html file, I just want a simple link/button that calls the test.php file. I don't want users to have direct URL access to the php file (that's why I put it outside the root directory), but I want other files like index.html to access it. I've tried web.config and .user.ini stuff for security (which haven't work), but I figure if this works, then security should be fine.

  • I've tried href="../test.php", and it doesn't work (404 - File or directory not found)
  • Permissions for test.php are the same as wwwroot (through the cp portal)
  • I don't want to include the php file, I want to call and run it separately
  • I've looked at every related post in stackoverflow for two days and nothing seems to work
  • If your suggestion is to use a full path to the file or create a virtual directory, please include an example. Because I've tried both with no luck
  • I've also read you would need php script to access php files outside the root, but have no idea how to call the outside file. I've tried this once, but got a 404 exception. Would be great if it worked

Does anyone have a simple way to accomplish this? (Especially anyone working with Windows Arvixe hosting). I feel like there should be a simple answer here ): Thanks for reading this far and thank you very much for any suggestions

1 Answers1

1

Some PHP script has to be web-accessible. If you don't want your main PHP script to be web-accessible, you still need another web-accessible PHP script to call the main script. There is no way around this unless you introduce an additional technology like SSI.

It makes no difference if you use Windows or not for what you're asking.

  • Ok, so let's say I add php script inside index.html to call test.php that's outside the root. How would I do that? What would that code look like? – Llamapuppies Nov 01 '14 at 03:29
  • 1
    You don't add a PHP script inside index.html. You make a new file called index.php and that is your default file. That will cause the index.php file to be processed by your web server before it is sent out. You would use either `require` or `include`. Please see http://www.tutorialspoint.com/php/php_file_inclusion.htm for examples. –  Nov 01 '14 at 03:32
  • What if I want to pass in data (like form data) with POST, then process it in the test.php file with GET. Will using require/include in the index.php file still be feasible? – Llamapuppies Nov 01 '14 at 03:39
  • Sorry, I meant call the test.php file with javascript while passing in form data, then getting it with GET to process – Llamapuppies Nov 01 '14 at 03:47
  • 1
    Sure. See http://php.net/manual/en/reserved.variables.get.php and http://php.net/manual/en/reserved.variables.post.php . Also, in PHP files that are `include`d or `require`d are in the same scope as the file that calls them but you can access $_GET and $_POST anywhere so it doesn't matter unless you've reassigned the values to other variables in which case it also doesn't matter due to them being in the same scope. –  Nov 01 '14 at 03:48
  • 1
    JavaScript runs on the client side. You can only call web-accessible PHP files (like index.php) using JavaScript. The web-accessible script can then call a script that is not web accessible using `require` or `include`. –  Nov 01 '14 at 03:49
  • Ohhh, that makes a lot of sense, I'll go test it out now. Thanks a lot! (Might add more comments if I can't get it to work, but thanks again) – Llamapuppies Nov 01 '14 at 03:54
  • No prob. If you have other questions not related to your original it might be better to ask a new question. Good luck! –  Nov 01 '14 at 03:56