PHP - get POST data within the include - php

So here is my situation:
I got a plugin directory that all needs the same functions. So i wrote a general.php with the common functions that is used by every plugin. Now i could let the developer of the plugin include my general.php file but this could be very annoying for him/her since its very nested so when a developer wants to include it he/she gets this: include '../../../../../../../lib/general.php' So what i did is let the plugin loader include the general file beforehand so that the developer wont need to access the general file every time it has a new file. Now this works and all until the developer does a form and needs to get POST data.
But i can't access the POST data inside the include, is there a way to access the POST data from the include file?
The loading kinda goes like this:
include '../lib/general.php';
include Plugin::GetPluginViewPath;

i can't access the POST data inside the include,
You can.
is there a way to access the POST data from the include file?
Yes. Just access it.
As long as $_POST array is populated and not unset by some code, it is perfectly accessible everywhere.
Of course you should include PHP files, not HTTP resources, though

Related

Automatically Include wp-load on Admin Plugin View Pages

I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.

Wordpress include php file

I am trying to create a php website on WordPress for the first time.
When I create a page, it creates a permalink which is of the form http://localhost/?p=123. I don't know if there is a corresponding file.
I've installed insert-php plugin to read php code. It works fine on a static page. But how do I include another php file? I want to include my_utilities which contains all the back-end functions, in login. It has a permalink 'http://localhost/?page_id=45'.
What to do?
include 'http://localhost/?page_id=45' doesn't work.
Pages you define in WordPress's backend are pages mostly setup for your users to view / read. They, by default, have this URL structure of http://localhost/?p=123 (though this can be changed, but that's a whole different lesson).
To include a script file, upload the file to your folder structure where you have your website then refer to it in your include statement as follows:
include('path/to/folder/my_script.php');
EDIT: You may also want to have a look into WordPress Page Templates:
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
For exapmle your wordpress file is at
/var/www/html/wp-content/myphpfiles/test.php
To include the test.php file in your wordpress page you have to following.
[insert_php] include('wp-content/myphpfiles/test.php'); [/insert_php]
Thats it.

Storing Scripts Securely

I have a website that serves two parties, buyer and sellers. So once i have authenicated the type of user i load the respective module. See logic below:
If $loggedinusertype = Buyer;
include(/buyer_module.php);
else
include(/seller_module.php);
Now the way i store these modules is just the way i would store a contact.php file. These modules can be accessed if i go to domain.com/seller_module.php. Now, i want to know how to store these modules in such a way that nobody could access it directly and can only be used in the include component. I have 200 of these modules....
You could store them in an area outside of your normal web directory.
Say your web directory is /home/yoursite/www
You could put your include files in /home/yoursite/some-other-directory and no one would be able to access them from your site directly.
I have two suggestions on how you could do this.
Just store all of the modules outside of the web root so there is no way they can be accessed from the browser.
If the above is not feasible, define a constant in your main application or in the script that includes the individual modules. In the individual modules, check to see if the constant has been defined. If it has not, then you can assume someone is trying to access it in the browser, if it is, then the file was included by your script.
Example of 2:
index.php
<?php
define('SOME_CONSTANT', 1);
// ...
include 'buyer_module.php';
buyer_module.php and all other modules you don't want called directly
<?php
if (!defined('SOME_CONSTANT')) exit;

wordpress get current user without wp-load.php

I am currently trying to write a couple of pages into my website that are not part of the wordpress site but I would like to be able to use the wordpress users. I have this working using the following code
require_once("../../../wp-load.php");
$current_user = wp_get_current_user();
Now I am able to use the $current_user variable for everything I need. However because I am includeing the wp-load.php file there is a lot of overhead that I really don't need.
My question is how can I get the current wordpress user without including wp-load?
I don't mind having to include a few extra files myself but I really don't need or want the entire wp enviroment to be set-up each time this page is called just so I can get the user.
What are you doing in those pages? I'm asking because, if you want to send some POST via ajax, you should check this.
If you don't use ajax, but you just need some pages where you can acces Wordpress functions, well somehow you must include wp-load because this file is loading Wordpress.
I suggest creating a file called page-custom-name.php in your theme folder, and publish an empty page with the exact title "Custom-name". Now when you'll be visiting www.yoursite/custom-name you will see that page, and you can get the current user info, or access other Wordpress functions.
Later edit:
This idea a partial solution: How about using hooks?
In your functions.php put these lines:
add_action('wp_login', 'aboutuser');
function aboutuser($username, $user)
{
$userObject = $user;
//find a solution to send this data to your url of your application
}
Basically, when users are logging in Wordpress, the aboutuser() function is executed, and it's get 2 params(in Wordpress 3.3; in earlier versions it gets only the username).
Now, when users are logging in we have acces to the wp user object. Maybe there is a way pass this data(a POST request) to your application and store it in a session.
I don't sure if it's possible though.

Should the PHP files that do processing/validation of data called by AJAX be put in 'include' folder and made inaccessible to the user?

My Question is suppose I have a form which needs validation through AJAX. The AJAX is sending data to to a file called do_ajax_validation.php. Now should I put this file in 'include' folder and name it do_ajax_validation.inc.php and bar it from direct access of the user or should I put it in the directory in which the original form resides?
Edit: And the same question is for the files which do processing of data of forms (or the files which are defined in the action property of the form tag)
You can't prevent the user from directly accessing the file. If you do, you prevent the XMLHttpRequest object from accessing it too!
You should have a reasonable URI for the XHR to access. Beyond that, structure your files in whatever fashion makes the site easiest to maintain.

Categories