I am running into a little problem with the below, I am trying to learn for some future projects and need a hand.
Here is the setup:
I have a user class in php in a models directory.
I have a home.php file in my root directory.
I have included the model for the user class in the home.php file and am able to access it just fine.
I have a profile.php file that I would like to load via ajax onto the home.php file.
I am able to load the profile.php file just fine, however the problem is that the info on that page which is just being called via <?php echo $user->first_name; ?> is not being displayed when I load the page via ajax. However if I load the page normally without ajax, it displays just fine.
What I have tried so far:
I have tried to include the user class in the profile page, however it is giving me an error that it is already included.
I would rather not place this into a session variable as I would like to be able to access the class from here.
I also need to be able to access the user class on other screens, that is why I have included it in the home.php file.
Any help would be greatly appreciated as I am rather new to working with php and ajax.
Thanks
Here is some of the code that I am working with.
home.php
//Here we include are models that we need access to.
include 'apps/user/model/user.php';
$my_user = new User();
global $user;
$user = $my_user->getUserById($_SESSION['loggedin_user_id']);
profile.php
<input type="text" value="<?php echo $user->username; ?>" class="input-xlarge">
javascript to load the form via ajax
$.ajax({
url: 'apps/user/view/profile.php',
success: function(data){
if(data != null) $("#content").html(data);
}
});
I also get the following error when I view the console.
[01-Jun-2013 10:38:03] PHP Notice: Undefined variable: user in /Users/btackett1377/development/Test/Base/apps/user/view/profile.php on line 23
Have you tried using require_once instead of include? I would try requiring the user class in both the home and profile pages.
For your reference, PHP has four functions for including files:
include - Blindly include the requested file with no regard for whether it's been included already. Warns you if the file is not found.
include_once - Ensure that the file is only included once. Also warns you if the file is not found.
require - Similar to include, but causes fatal error if the requested file could not be found.
require_once - Similar to include_once, but causes fatal error if the file is not found.
Related
I have the following issue. I want to load the Wordpress environment into an external php file. All my work is currently on a localhost and my php code is as follows:
<?php
require("../../wp-blog-header.php");
$admin = do_shortcode("[su_user field='user_email']");
if ($admin === "admin#email.com") {
// Do stuff here
}
else {
echo "<h1>Forbidden.</h1>";
}
This works perfectly fine in my localhost, so I figured that when I upload the file to the server it must work too, given that the file is stored in the same directory as it is locally, hence ../../wp-blog-header.php. Sadly, this does not work. When I echo $admin; the following error appears:
User: user ID is incorrect
I tried using the native Wordpress functions without using the shortcode like this:
<?php global $user_email;
get_currentuserinfo();
echo $user_email;
?>
This does not work either. Is there a more efficient way to load the Wordpress environment into an external php file? Any suggestions on what is happening here? It does not necessarily have to be an e-mail, it could be the user id as well, which I tried and gives me 0.
I forgot to mention, the php file loads and echoes Forbidden together with the error. This makes me believe that the file require("../../wp-blog-header.php") is actually being loaded because when I change the directory I directly get an error that the file is now working/available.
The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user.
UPDATE
My root is:
/var/www/vhosts/test-website.com/httpdocs/
Which I tested with require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php") as well. Still same result. The weird thing is that if I add wp_head() below this line, the header gets loaded. I reformulate my question:
How can I get the current e-mail address of a logged-in user in an external php file?
If your file is same folder root with Wordpress instance. just use code bellow:
<?php
define( 'ROOT_PATH', dirname(__FILE__) );
require(ROOT_PATH . "/wp-blog-header.php");
//your stuff
I am trying to call a function which is defined in functions.php. But I can’t figure out why this error is coming.
Let me tell you in detail what I am trying to accomplish: there are two header files — client and admin. The client header will be accessed when the user enters into a page through index.php, but admin header will be accessed after an admin successfuly logged in.
In the functions.php this is the function I have defined:
function get_my_header(){
if(is_home()){
get_header('client');
}
elseif(is_page('dashboard')){
get_header('own');
}
}
from index.php get_my_header() is called with no error but when I try to access the same function from the admin page I am getting this error:
Fatal error: Call to undefined function get_my_header()
The page where this error is coming has only this one line:
<?php get_my_header(); ?>
The most common causes of this are:
The file you think is being included, isn't being included.
Your calling the function prior to including the file
Based on your description as a header a file - I think the first is most likely. As you also mention that it's in an admin page that it doesn't work on - does the admin page live in a sub directory e.g:
httpdocs/
index.php
admin/
admin_index.php
If it does - the file path to the include file may be wrong, depending on your error_reporting() settings you may or may not be alerted to this.
One way to debug this would be to use require() instead of include() on the functions file - as that creates a fatal error if PHP can't find the script.
http://php.net/manual/en/function.require.php
I have followed MVC Pattern In my PHP pages. I have created a controller page for every view page and which will interact with Model page. I have checked the Login of the User on the Top of the every View page. But I have Never checked in Controller page. Since It is possible to anyone enter directly to controller page and may change database content.
So anyone please tell me, is it possible to enter into database through controller page. And whether I need to check Login of the User in controller page.
If I understand correctly, you are putting a LOGIN check at the top of every linked page from your controller page (assuming your index.php).
If so, then you don't need to do a login check at the top of every one of your linked pages, only from the index.php. If your login succeeds at the top of your index.php, then you continue on to include your destined view page.
For example (in your index.php)
<?php
if ( !$user->checkSession() )
header('Location: login.php');
if ( $_GET['p'] == 'viewPageName' )
include('modules/viewPageName.php' )
elseif and so on
?>
EDIT
Now I have a better understanding of your question.
Solution 1:
In your index.php (place at top)
<?php
define('DIRECT', true);
your login check, etc...
?>
In your other files, put at the top
<?php
if (!defined('DIRECT')) die('No direct access is allowed');
other code, etc...
?>
Solution 2:
Put an .htaccess file in the folder where all your other files are located, and deny direct access to those files.
Put in the .htaccess the following:
deny from all
Solution 3:
Well, assuming you've defined the $db file at the index.php and have set the class up in your index.php, your other files will return errors because you didn't define your DB class in them.
In other words, if you defined $db = new Database(); in your index.php, your other files will get an error if you try to access directly because $db has yet to be defined in them.
Im loading the php rss parsing library simplepie onto my site and i include it in the header.php file like so:
<?php
// Make sure that SimplePie is loaded
require_once('inc/simplepie.inc');
$feed = new SimplePie('rss.com'); <-- This is a example url
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
?>
Then i try to access the object from another page called page.php and it gives me this error:
Fatal error: Call to a member function get_items() on a non-object in /home/callofdu/public_html/wp-content/themes/Starkers/page.php on line 13
I get the error from this line of code:
<?php
foreach ($feed->get_items() as $item)
{}
?>
Its weird because if i include both of those chuncks of code together on the same php page it all works fine.
I just am not understanding something, please help.
Several things could be wrong here:
Code order is incorrect.
Realise that when you require or include a PHP file, the entire code of that file will be executed at that point. In other words: interpret the command require_once as "Insert file contents here", and check whether the order of lines of code are still right.
You are calling page.php in a separate request.
If you use your browser to manually surf to page.php after the completeion of the previous page, $feed will no longer exist. If you do want it to live on between requests, do the following:
a. Replace all instances of $feed with $_SESSION['feed']
b. In the first line of every file that you surf to (so not the require'd files), put session_start();
The require and include calls in php basically take the contents of that file and slap in place.
So if you follow the linear path that php taking while running a script, you have to make sure that the library is called before you use it.
Basically in practice, if you are executing page.php as a standalone then you will need to require it in that script also, however, if page.php is being included in the first script you mentioned then you don't need to require it again, but make sure the library is included before page.php
ie
<?php
// Make sure that SimplePie is loaded
require_once('inc/simplepie.inc');
$feed = new SimplePie('rss.com');
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
require_once "page.php";
?>
doesn't need to be required in both
As for the differences between the require and include calls check out
http://www.php.net/manual/en/language.control-structures.php
EDIT:
After seeing this is Wordpress related, remember variable scoop. Variables do not inherent from a functions parent. So in page.php try:
<?php
global $feed;
foreach ($feed->get_items() as $item)
{}
?>
You need to include the libraries on EVERY page you're using the library's functions.
I think it is relative paths issue. Try using absolute paths. Or check if path to rss.com is okay. May be you need to call it in another way: $feed = new SimplePie('../rss.com');
I have an index.php that includes config.php. Now I'm using $.get() when I click a button to load a form:
$.get('form.php', function (form) {
$(form).insertAfter();
});
This form requires config.php for some input values; however, when the form has loaded, I receive the php error:
Notice: Undefined variable: config in form.php on line 27
Line 27 of form.php: if ($config['spam_protect']) {
I realize the config.php has already been instantiated when the page initially loads, thus I suspect this to be the problem I'm having. I've tried including config.php within the form.php file to no avail. Tips or tricks anyone?
Thanks!
Notice: Undefined variable: config in form.php on line 27
The message is quite clear: $config isn't loaded, maybe because your config file doesn't load correctly.
Note that if you make an Ajax request, the script requested in that request is an entirely new PHP instance. You may need to include config.php in that one as well.
Either form.php doesn't include config.php, or config.php doesn't declare $config
You should be including config.php in form.php if it's needed.
Calling it through ajax has nothing to do with index.php, it's run independently.
That being said, we need some more info on this one.