Linking to files in different directory in PHP - php

My other question about getting help with the programming side of wordpress was labeled off topic for some reason so I'm asking a different way. I'm trying to embed my wordpress posts. I'm using this tutorial:
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
The problem is with this code:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('./wordpress/wp-load.php');
query_posts('showposts=1');
?>`
When I try to run the page that this is inserted into, I get the error saying that file doesn't exist. Pretending my domain is blah.com, the file is in www.blah.com/wordpress/wp-load.php and the page that includes this PHP code is in www.blah.com/other/page.php.
How do I change the syntax of the link on the require line to make sure it's pointing to the right place since right now it doesn't seem to be working?

have you tried with
require('../wordpress/wp-load.php');
or anyway something like
require('../../wordpress/wp-load.php');
?
(depending on the depth of your file position)

Just like the above answer
This worked for me
require('../wordpress/wp-load.php');

Related

Including File PHP not work - work by using comment tag - bug?

ISSUE:
PHP Warning: ........ failed to open stream: No
such file or directory in
/home/.
I found many titles discussed related on the topic I pick now. I tried to look into the path and make sure everything went okay. I also checked the server connectivity, bandwidth or everything related to hosting or DNS stuff and tried it again. But it was still not working.
However, when I use php comment tag, as in #, unbelievable, it really really works! That's why I want to find out why it could be like that.
Here's the actual path:
/home/appacybe/public_html/in-grammar/user_validator.php on line 2
Previous (error) report codes:
<?php
include('../user_validator.php');
...
?>
<!DOCTYPE html>
...
</html>
Working codes:
index.php:
<?php
################# start ####################
############################################
################# end ####################
include('../user_validator.php');
...
?>
<!DOCTYPE html>
<!--bla bla
./end -->
Inside user_validator.php:
<?php
echo 'hallo';
?>
Successfully Output:
- Hallo
- other text below it.
Some suggests to use Magic Constant and many says that I should use require, require_once or others.
But when I use that php commentary tag, I found it works successfully.
What's wrong? Is this a bug? or I just don't know how to be on the right PHP programming?
Please let me know.
This is the simple thing for you perhaps but really I still question; what's wrong with it or "me" ?
NEW POSSIBILITIES:
I tested to VPS or Dedicated Server and it works by using common function. But always got failed in share hosting - but working with the comment tag.
What's going on? :(
Any help would be so great!
You can use dirname(FILE) to obtain the path of the script that line is called in. You can then reference relative paths from there e.g.
include (dirname(__FILE__) . '../user_validator.php');
According to me, In some cases where $_SERVER['DOCUMENT_ROOT'] is not set or is not what you would expect (i.e. not set in CLI or old IIS, or invalid in certain CGI setups) then server might not be able to find out related path of include file. So, it will give an error.

PHP include code for if file exists and what to do if it doesn't

Long ago, this code used to work but now it seems something is going on preventing it from functioning properly. I'm hoping somebody can tell me whats going on. I'm concerned upgrades to PHP have killed this code. Or has it?
I use this code (posted below) to check and to see if an html file exists, and if it does it'll use it. If not, it will use the file index2.html.
<?php if ((file_exists("$id.html")) == true) { require ("$id.html"); } else { require ("index2.html"); } ?>
I use this code on my homepage, index.php. However for some odd reason, when I type in a link: example: index.php?id=exampleurlhere, the code isn't checking to see if the file exists and is automatically using the index2.html file, despite my code telling that if the file exists (which it does), then it's required to use it. Why is it now ignoring my command?
Been using this code for years and never had any problems with it until recently it seems. Any suggestions to fix it?
I think the problem lies in the "register_globals" setting. This used to be on by default in the (very) older versions of PHP, but has been removed in the newer versions, as this caused a lot of variable injection attacks in the old PHP.
Your $id came directly from the value in the URL. Now that it no longer auto registers, you can add in a line "$id=$_GET['id'];" just above that line to get it to work again.
This is just a quick fix. I suggest you rewrite the program so that there will not be any change of users accessing files illegally using the URL.

500 Internal server error with new php files on server (Wordpress/Woocommerce)

I am getting a strange 500 Internal Server Error with a new script I am trying to implement in the actual site. Here's a screen:
![500 Internal][1]
I can route to this files manually without problems and they are working too. But not in the script itself. The Paths are also correct.
Heres the link to the Site:
[>>> Link <<<][2] (just enter R10369 in the input field or a random number)
Everything else is working correctly except these 3 files:
reseller.php,
checkresellerid.php,
resellermail.php
I googled a bit and everywhere is the .htaccess mentioned. but I never modified it or overwrited it. What could be the Problem? Thanks for any Help and sorry for my bad Englisch.
(Let me know if you want to see the php files)
EDIT: I managed to include my new php files into wordpress but i still got the 500 Error
I checked out the website.
I think Wordpress doesn't let you call .php inside of it's system.
I mean you cannot call PHP files for ajax.
You need to use wordpress ajax. Here is a snippet how to use ajax:
Function.php in your theme file.
function myajax()
{
//do stuff
die();
}
add_action( 'wp_ajax_nopriv_product_s', 'myajax' );
add_action( 'wp_ajax_product_s', 'myajax' );
And in your javascript file using jQuery:
The url may change, maybe it's enough to have wp-admin/admin.ajax.php or something like this, i don't really remember right now.
$.post('/wp-admin/admin-ajax.php',{action:'myajax',yourdata:"mydata"}).done(function(data)
{
//do stuffs
});
Update:
So basically if you want to have ajax request inside wordpresss, you need to define these things and use it like this. the "action" parameter is the function name which you want to call. And you need to put the PHP code into your current theme's function.php.

Including plugin files into main plugin file properly

I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.

How do I use require_once()?

I create a PHP page with the following content:
<?php session_start(); ?>
<?php require_once(./classes/MaterialUtil.class.php);
$mUtil = new MaterialUtil();
?>
I put the MaterialUtil.class.php file in D:\xampp\htdocs\drupal\sites\all\themes\zeropoint\classes, but I get the following error message:
Parse error: syntax error, unexpected '.' in D:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code on line 7
Could you please tell me what I do wrong?
The error is caused from the fact you didn't use a string for the filename, and PHP understood the dot as being the concatenation operator; as such, because there wasn't any value before the operator, PHP gave you an error saying it found the concatenation operator in the wrong place.
As kalabro said, the correct code is the following one:
<?php session_start(); ?>
<?php require_once('./classes/MaterialUtil.class.php');
$mUtil = new MaterialUtil();
?>
This is the part of the answer that is not strictly related to Drupal.
What you are doing is not what I would suggest to do, for two reasons:
You are putting the "classes" directory in the wrong place. Those files are not related to the theme being enabled, but they are related to the page being viewed. Even if you have a single theme, and users are not allowed to select a theme for themselves, it still wrong to put those files in a theme directory.
Putting the files in the directory containing a theme, which will needs to be updated when a new version is available, could cause you to lose the additional files you added, if you are not careful.
Executing PHP through eval() to, e.g., get content to show in a node is not something that you should do. This is because:
As you have used the PHP filter for the node, the node becomes only editable to a restricted group of users. (I would not suggest to allow untrusted users to use PHP as input format)
When you have PHP code that you need to execute, it is always better to create a custom module that is enabled for the site.
If you were trying to include a PHP file from inside a module, then you should use module_load_include(), instead of require_once(), as already suggested by marcvangend.
XAMP server does not run on windows file system format. You must write your file location like localhost/xyz/abc ..

Categories