Splitting Up Code

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
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Splitting Up Code

Post by bhempel »

I am trying to split up some php code. When I do I keep getting a Parse error. The following is the code I am trying to split up:
<?php
/**
* User has already logged in, so display relavent links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
if(MAIL){
$q = "SELECT mail_id FROM ".TBL_MAIL." WHERE UserTo = '$session->username' and status = 'unread'";
$numUnreadMail = $database->query($q) or die(mysql_error());
$numUnreadMail = mysql_num_rows($numUnreadMail);

echo "[<a href=\"mail.php\">Inbox ($numUnreadMail)</a>]";
}
?>

[<a href="userinfo.php?user=<?php echo $session->username; ?>">Profile</a>]&nbsp;[<a href="useredit.php">Edit Account</a>]
<?php
if($session->isAdmin()){
echo "[<a href=\"admin/admin.php\">Admin Center</a>]&nbsp;";
}
echo "[<a href=\"process.php\">Logout</a>]";?>


<?php
}else{
?>

<form action="process.php" method="POST">
<fieldset id="login">
Username:<input type="text" class="login" name="user" value="<?php echo $form->value("user"); ?>"><?php echo $form->error("user"); ?><br />
Password:<input type="password" class="login" name="pass" value="<?php echo $form->value("pass"); ?>"><?php echo $form->error("pass"); ?><br />
<input type="checkbox" name="remember" <?php if($form->value("remember") != ""){ echo "checked"; } ?>> Remember me.

<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"><p />
<a href="forgotpass.php">I forgot my password</a> |
<a href="#">Resend activation e-mail<p /></a>
</fieldset>
<?php
if(EMAIL_WELCOME){
echo "<p>Do you need a Confirmation email? <a href='valid.php'>Send!</a></p>";
}
?>
</form>

<div class="clear"></div>

<h1>Register</h1>
<p>Use of this site does not require you to be a registered user. Before you register please ensure you are familiar with our terms of use and related policies.</p>
<a href="#">Terms of Use</a> | <a href="#">Register</a>

<?php
}
?>
Basically I want to take the first half:
<?php
/**
* User has already logged in, so display relavent links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
if(MAIL){
$q = "SELECT mail_id FROM ".TBL_MAIL." WHERE UserTo = '$session->username' and status = 'unread'";
$numUnreadMail = $database->query($q) or die(mysql_error());
$numUnreadMail = mysql_num_rows($numUnreadMail);

echo "[<a href=\"mail.php\">Inbox ($numUnreadMail)</a>]";
}
?>

[<a href="userinfo.php?user=<?php echo $session->username; ?>">Profile</a>]&nbsp;[<a href="useredit.php">Edit Account</a>]
<?php
if($session->isAdmin()){
echo "[<a href=\"admin/admin.php\">Admin Center</a>]&nbsp;";
}
echo "[<a href=\"process.php\">Logout</a>]";?>


<?php
}else{
?>
And seperate it from the second half putting it in its own file and than linking it back to the other one usingthe include function. Everytime I try to do that though I get an error. It doesn't matter where I split the code. Is there some sort of function I can use to correct this?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Splitting Up Code

Post by califdon »

Sounds like your problem is coming from the way you are including the file, which you haven't showed us. Remember that when you "include" a file, it is copied verbatim, so, for example, if your include file starts off with "<?php" and since you have to already be in a php code block in order to call include("...");, you would end up with 2 successive opening tags. Look for that sort of thing.
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Re: Splitting Up Code

Post by bhempel »

for those of you who are confused let me try and clear things up. I want to create 3 different files (login.php, header.php, footer.php). The login.php file will basically just have include statements (this is just temporary; I am only trying to get the code to split up so everything is very plain). Header.php would include the first part of the php code posted earlier which would display [Profile] [Edit Profile] [Inbox] [Logout] when the user has been logged in. The second half will be within footer.php and according to the current code will only be displayed when the user is not logged in. Again, both files will be included in login.php.

The reason behind doing this is because in the actually site design I have I need the [Profile] [Edit Profile] [Inbox] [Logout] displayed in a menubar at the top of the page, while the login fields will be displayed in the body portion.

If anyone has any further questions please feel free to ask.

bhempel
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Re: Splitting Up Code

Post by bhempel »

califdon wrote:Sounds like your problem is coming from the way you are including the file, which you haven't showed us. Remember that when you "include" a file, it is copied verbatim, so, for example, if your include file starts off with "<?php" and since you have to already be in a php code block in order to call include("...");, you would end up with 2 successive opening tags. Look for that sort of thing.
Thanks for the advise I am still new to php so I never thought of that. I am currently using the include file as such:

<?php include("header.php"); ?>

How instead should I be using it?

bhempel
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Re: Splitting Up Code

Post by bhempel »

I am thinking that this is not the problem because I am displaying it as below so I am closing out the php each time.

This is an example of login.php as of now.

<?php
$page = "test.php";
include("include/session.php");
?>

<?php include("header.php"); ?>
<?php include("footer.php"); ?>
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Splitting Up Code

Post by robnet »

Can you show the individual files you've created?

I suspect you've got something running through the included files that shouldn't be. For example you can't start an if statement in your parent file and then close it in an included file.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Splitting Up Code

Post by califdon »

bhempel wrote:I am thinking that this is not the problem because I am displaying it as below so I am closing out the php each time.

This is an example of login.php as of now.

Code: Select all

<?php include("header.php"); ?>
<?php include("footer.php"); ?>
Do you see that when the parser comes to the first line above, it begins parsing PHP, encounters the include() function, executes it, and if that copies another <?php without there having been a closing ?>, it will parse incorrectly? That's what I'm talking about.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Splitting Up Code

Post by robnet »

califdon wrote:
bhempel wrote:I am thinking that this is not the problem because I am displaying it as below so I am closing out the php each time.

This is an example of login.php as of now.

Code: Select all

<?php include("header.php"); ?>
<?php include("footer.php"); ?>
Do you see that when the parser comes to the first line above, it begins parsing PHP, encounters the include() function, executes it, and if that copies another <?php without there having been a closing ?>, it will parse incorrectly? That's what I'm talking about.
If there isn't a '<?php' within the first include it will treat it as html. Each included file is essentially treated as a standalone file. The only difference is that it will have access to the same variables, classes, etc that are available at the point the include is processed in the parent file. And any variables, classes etc created within the include will then be available to the parent once the included file has been completed.

I think we need to see the code after it's been split, there's something hidden in the shadows that will give us the solution :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Splitting Up Code

Post by califdon »

I stand corrected. In any case, I agree that the issue is with what's in the included file, which we need to see.
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Re: Splitting Up Code

Post by bhempel »

I have attached a zip file of the entire login system. If it will help you to see it running, simply use WAMP Server to view it, or upload it to a site if possible. Remember to edit constants.php to fit your database needs. Also Either way all of it is here. I hope that helps. working.php is a working version of it. You can upload dbtables.sql to your database to setup your tables automatically.

Note: I used test.php instead of login.php..
Attachments
test.zip
(21.57 KiB) Downloaded 12 times
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Splitting Up Code

Post by robnet »

Thanks for the code.
You're getting an unexpected end of file in header.php:
It looks like you've started an 'else {' in header.php and the close '}' is in footer.php. If you open braces { you have to make sure it's closed within the same file.

Without a rework of the code the easiest way to fix it would be to tweak where the includes sit:
Since the whole of footer.php is within the else statement I suggest adding the include for this into header.php rather than in test.php as it is at the moment.. So, at the end of header.php, after <div id="maincontent"> add this:

Code: Select all

<?php include("footer.php");
} ?>
Then take the footer.php include out of test.php and remove the close brace at the end of footer.php.

Does that make sense?! It's late 8O

At that point the page seems to do what is required. Note, I didn't add the database stuff and just commented out the session.php include but I think everything should work once you've made those changes.
bhempel
Forum Newbie
Posts: 8
Joined: Wed Aug 12, 2009 1:26 pm

Re: Splitting Up Code

Post by bhempel »

Thank you robnet and califdon for your input. After reading the below post from robnet I took the problem to the current developer of the JP77 Login System Ivan Novak (http://www.ivannovak.com) who left off where the original developer ended. He was able to tweak the code to allow this to function to work correctly.
robnet wrote:Thanks for the code.
You're getting an unexpected end of file in header.php:
It looks like you've started an 'else {' in header.php and the close '}' is in footer.php. If you open braces { you have to make sure it's closed within the same file.

Without a rework of the code...
Thanks again to all who where involved in fixing this problem.

bhempel
Post Reply