0

A client has asked us to add an editable page to their site which they can update via external cms. No drama there; I can call this easily with the include "filename.php"; function but where I'm getting stuck is this external file needs to be behind a login so unless I include it as part of an existing $usermessage variable it's just constantly displayed whether logged in or not.

I'm having to edit an existing, previously built site (which is a few years old) so I am kinda stuck with the current way it's setup but is there a way I can include the file as part of the $usermessage?

See code below and thanks for any help...

<?php
require_once "header.php"; 
#require_once "onlinearchive.php";

if (isset($_SESSION['loginid']) && isset($_SESSION['email']))
{
    $session_email = $_SESSION['email'];
    $query = sprintf("SELECT name FROM login WHERE email = '%s' LIMIT 1",
    mysql_real_escape_string($session_email));

    $result = mysql_query($query);
    $username = '';
    $usermessage = '';
    $intevents = '';

    while ($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        $username = $row['name'];
    }

    if ($username)
    {
        $username = 'Welcome ' . $username;
    }
    $usermessage = '<p><span class="green"><strong>'.$username.'</strong></span>
<strong> to the Member’s Area. </strong><br><br>Here you’ll be able to view 
news and notifications unique to IPA Members, so please check back regularly 
for updates.<br><br></p>';

}
else
{
    $usermessage = '<p><span class="green"><strong>To access the Member’s Area, 
please <a href="login.php">Log in</a> or <a href="login.php">register</a>
</strong></span><br>';
}

?>
<div id="page-title"><p>IPA Member’s Area</p></div>         

<div id="container" class="clearfix">


    <div id="content">
        <?php echo $usermessage; ?>


    </div> <!--end content-->
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
eanno
  • 27
  • 3
  • Why not just define a boolean variable before checking the session and set that to true / false wether the user is authenticated or not. After that, depending on it's value, you can choose wether to include the page or not after echoing the $usermessage. – Sebastian Apr 13 '17 at 10:52
  • Thanks for the feedback Sebastian. My php is somewhat limited so any direction on syntax would be appreciated. Thx – eanno Apr 13 '17 at 11:09
  • Actually, whilst I haven't exactly solved it, I've 'got round it' by moving all the existing content from the variable into the include file and then calling the variable by itself with everything contained in that. My problem was I couldn't get the variable to display both the html and the include together (as a string?). I'm still not sure if what I was trying to do is possible but my workaround is a means to an end...right?! Thanks again for your help. – eanno Apr 13 '17 at 12:04
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 13 '17 at 13:22

1 Answers1

0

If your file filename.php is structured like this:

Example of filename.php:

<?php
//Example of filename.php

$usermessage_to_return = "here the message to return";
return $usermessage_to_return;

Import the content of filename.php on another file:

<?php
//Example to import filename.php

$usermessage = require 'filename.php';
echo $usermessage;
//here the message to return

The trick is the return on filename.php.

borracciaBlu
  • 4,017
  • 3
  • 33
  • 41