FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » php include question
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
php include question [message #172149] Wed, 02 February 2011 00:14 Go to next message
Bill[1] is currently offline  Bill[1]
Messages: 1
Registered: February 2011
Karma: 0
Junior Member
I have many (hundreds) html files that I need to put some logic to
check whether the user logged in or not. So I add php code to check
the cookie, which is set when the user sign in. The following is the
sample php file.

============================================================
<?php
if ( (!isset($_COOKIE['uid'])) )
{
?>
<html><head><title>you are not logged in</title></head><body>
You are not logged in so you are not able to access this resource. </
br>
</body></html>
<?php
}
else
{
?>
<!-- the cut line for the header -->
<html>
<head>
<title>Logged in</title>
</head>
<body>
you are logged in and the resources is here and blah blah...

</body>
</html>

<?php } ?>

====================================================
I am trying to create a header.php with the first lines all the way to
the <!-- the cut line for the header -->
and then include it in the other php files.

The reason I want to create a header.php file is that the real logic
is actually more complex than this example. I will need to only
change one file instead of hundereds or thousands files when we need
to change the logic. And also for the new pages, I can ask the
developer to add only one line in each of the html file like:
<?php include('header.php'); ?>

But when I access the php file which includes the header.php, in the
error log, it says:

PHP Parse error: syntax error, unexpected $end in /var/www/html/
library/header.php on line 13
I know that is probably because in the header.php the if - else is
not closed yet. Here come my questions:

1) any good way to do what we want to do?
2) seems to me that the included php file should be logically self-
contained? - in the example, the if - else logic is split into two
files...

Thanks.
Re: php include question [message #172150 is a reply to message #172149] Wed, 02 February 2011 00:42 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/1/2011 7:14 PM, Bill wrote:
> I have many (hundreds) html files that I need to put some logic to
> check whether the user logged in or not. So I add php code to check
> the cookie, which is set when the user sign in. The following is the
> sample php file.
>
> ============================================================
> <?php
> if ( (!isset($_COOKIE['uid'])) )
> {
> ?>
> <html><head><title>you are not logged in</title></head><body>
> You are not logged in so you are not able to access this resource.</
> br>
> </body></html>
> <?php
> }
> else
> {
> ?>
> <!-- the cut line for the header -->
> <html>
> <head>
> <title>Logged in</title>
> </head>
> <body>
> you are logged in and the resources is here and blah blah...
>
> </body>
> </html>
>
> <?php } ?>
>
> ====================================================
> I am trying to create a header.php with the first lines all the way to
> the<!-- the cut line for the header -->
> and then include it in the other php files.
>
> The reason I want to create a header.php file is that the real logic
> is actually more complex than this example. I will need to only
> change one file instead of hundereds or thousands files when we need
> to change the logic. And also for the new pages, I can ask the
> developer to add only one line in each of the html file like:
> <?php include('header.php'); ?>
>
> But when I access the php file which includes the header.php, in the
> error log, it says:
>
> PHP Parse error: syntax error, unexpected $end in /var/www/html/
> library/header.php on line 13
> I know that is probably because in the header.php the if - else is
> not closed yet. Here come my questions:
>
> 1) any good way to do what we want to do?
> 2) seems to me that the included php file should be logically self-
> contained? - in the example, the if - else logic is split into two
> files...
>
> Thanks.

No, don't try to split the if -else logic into two files. You need to
keep it all in one file.

And BTW - cookies are not a good way to see if someone is logged in. A
hacker can easily create a cookie with an administrator's id and have
all kinds of access. Rather, use a session (since the session id, while
stored in a cookie, is a long, pseudo-random string which changes with
each session).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php include question [message #172157 is a reply to message #172149] Wed, 02 February 2011 08:15 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 02/02/2011 1:14, Bill escribió/wrote:
> I have many (hundreds) html files that I need to put some logic to
> check whether the user logged in or not. So I add php code to check
> the cookie, which is set when the user sign in. The following is the
> sample php file.
>
> ============================================================
> <?php
> if ( (!isset($_COOKIE['uid'])) )
> {
> ?>
> <html><head><title>you are not logged in</title></head><body>
> You are not logged in so you are not able to access this resource.</
> br>
> </body></html>
> <?php
> }
> else
> {
> ?>
> <!-- the cut line for the header -->
> <html>
> <head>
> <title>Logged in</title>
> </head>
> <body>
> you are logged in and the resources is here and blah blah...
>
> </body>
> </html>
>
> <?php } ?>
>
> ====================================================
> I am trying to create a header.php with the first lines all the way to
> the<!-- the cut line for the header -->
> and then include it in the other php files.
>
> The reason I want to create a header.php file is that the real logic
> is actually more complex than this example. I will need to only
> change one file instead of hundereds or thousands files when we need
> to change the logic. And also for the new pages, I can ask the
> developer to add only one line in each of the html file like:
> <?php include('header.php'); ?>
>
> But when I access the php file which includes the header.php, in the
> error log, it says:
>
> PHP Parse error: syntax error, unexpected $end in /var/www/html/
> library/header.php on line 13
> I know that is probably because in the header.php the if - else is
> not closed yet. Here come my questions:
>
> 1) any good way to do what we want to do?

You don't need the else part. Just show whatever error message you want
and exit the script.

<?php

if( !$logged_in ){
// ...
exit;
}

> 2) seems to me that the included php file should be logically self-
> contained? - in the example, the if - else logic is split into two
> files...




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: What *tasks* are hard for PHP?
Next Topic: Translate business information for free
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 20:46:44 GMT 2024

Total time taken to generate the page: 0.02131 seconds