Including a page that already has includes in it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Including a page that already has includes in it

Post by Smudly »

Hello,

I have a page called rates.php, that calculates the rate a user earns credits on my website. Inside this rates page, I've included my connect.php page to connect to the database, and also included admin.php which check if the user is an admin. If they are not an admin, it redirects them to the member's page. I am trying to include this rates.php page inside my stats.php page. Inside the stats page I need to do a calculate to determine the user's rate, and echo it out to the screen.

Now here is the issue. If I include rates.php, it is going to include the admin.php & connect.php pages. This is going to cause problems. How do I include only the code after my includes inside my rates.php page? I need it to ignore the admin.php page and connect.php page only when it is included inside stats.php

Any suggestions?
The code is show below:

rates.php here:

Code: Select all

<?php
session_start();
/// Include Admin Page Here ///
include('inc/admin.php');
include('inc/connect.php');

$user = $_SESSION['username'];

$sql = mysql_query("SELECT * FROM `users` WHERE `username` ='".$user."'");
$row = mysql_fetch_assoc($sql);

$sql2 = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'") or die(mysql_error());
$row2 = mysql_fetch_assoc($sql2);

$member = $row['member'];
$level  = $row2['level'];

$addrate = .005;

if ($member == 0) {
    $rate = ($level*$addrate+.50);
	$rate = number_format($rate, 3, '.', '');
}

if ($member == 1) {
    $rate = ($level*$addrate+1);
	$rate = number_format($rate, 3, '.', '');
}

if ($member == 9) {
    $rate = ($level*$addrate+1);
	$rate = number_format($rate, 3, '.', '');
}

?>
stats.php here:

Code: Select all

<?php
session_start();
if(isset($_SESSION['username'])){

$username = $_SESSION['username'];

include('inc/connect.php');
include ('rates.php');
    $statssql = mysql_query("SELECT * FROM `userstats` WHERE `username`='$username'"); 
    $row = mysql_fetch_assoc($statssql); 


}
else{
	header("Location: index.php");
}
?>
<html>
<head>
<title>Stats</title>
<link rel="stylesheet" type="text/css" href="styles/stats.css" />
</head>
<body>

</body>
</html>
admin.php here:

Code: Select all

<?php
// Include this file for Admin Only Pages

//session start and get variable

session_start();
$user = $_SESSION['username'];


include('inc/connect.php');

//query
$get = mysql_query("SELECT * FROM users WHERE username='$user'");
while ($row = mysql_fetch_assoc($get))
{
$admin = $row['member'];
}


if ($admin!=9)
	header('Location:http://www.mysite.com/index.php');

?>
Also, if there is a better way to check if the user is an admin, rather than including the admin.php page, please let me know.

Thanks!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Including a page that already has includes in it

Post by JakeJ »

Try include_once()
User avatar
morris520
Forum Commoner
Posts: 60
Joined: Thu Sep 18, 2008 8:56 pm
Location: Manchester UK

Re: Including a page that already has includes in it

Post by morris520 »

I think you've done it slightly over complicated.
Try include_once and require_once.
see php manual: php.net/manual/en/function.include-once.php
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Including a page that already has includes in it

Post by liljester »

I think you need to rethink how you include your files. so here are a few suggestions.

with any project that will have more than a few pages, i start with a common file that includes things that every page needs, database connection, site settings, security checks, navigation bar, etc

when i have a project that requires authentication/security, i typically put this into the common file as well, as you SHOULD check user security on EVERY page load to determine if the user is logged in and thier rights. navigation (aka menu) should be determined by user rights as well, i typically do this with an array. once the user's rights are identified, then you base the content presented to them on this.

now if you really want to, you can seperate out things like database connection, navigation, and site settings, just remember to include them all in the common file, and not in the content files. this helps you build a flexible frame work for your pages.

you can also use functions inside your content files.. show_credits() for example. your rates file should not include a db connection and things of this sort because it has already been done in your common file. now keep in mind, you may want to show different content based on the users rights, wich you will have to code into the content files.

so a quick example would be:

#common file: this file is never called directly from the address bar
create database connection
check security: is the user logged in? does the user have rights to view this page? if not, redirect.
do navigation of some sort based on what the current user's rights (if you need it)
put other small common fuctions here that you may use in any of your pages, i typically have a timer and error reporting functions

#content file: this is the file that is called in the browser, via directly or by link.
include common file
present your content based user rights


hope this helps.. and makes sense :)
Post Reply