简体   繁体   中英

Include php include inside a variable

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-->

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM