...

View Full Version : include text file



hans_cellc
10-18-2012, 08:04 AM
Firstly I know there are probably better ways to do this but I need to include my links within a text file.

I required the following file first in php:
config.php

<?PHP

// Inialize session
ob_start();
session_start();

// Create a function to shorten code on other pages to check if SESSION isset
function loggedin() {
if (isset($_SESSION['user_id'])) {
return true;
}
else {
return false;
}
}

function getuserinfo($field) {
$query = "SELECT $field FROM tb_user WHERE id ='".$_SESSION['user_id']."'";
if ($result = mysql_query($query)) {
if ($query_result = mysql_result($result, 0, $field)) {
return $query_result;
}
}
}


?>

Then I include the
header_secure.txt

<h1 style="font-size: 400%; font-family: Forte, Verdana, Arial, Helvetica, sans-serif; color: #000000; margin-top: -7px;" align="center">CONSOLE GAMES FOR SALE</h1>
<h1 style="font-size: 300%; font-family: Forte, Verdana, Arial, Helvetica, sans-serif; color: #FF0000; margin-top: -20px;" align="center">PS2 - PS3 - Wii - X-BOX</h1><br />

<!-- Links -->
<table width="100%" style = "margin-top: -25px;">
<tr align="center"><td ><h3 style = "margin-top: -5px; margin-bottom: 1px;"><a href="http://127.0.0.1/exam/index.php" name="top">Home Page</a> |
<a href="http://127.0.0.1/exam/insert.php">Insert</a> | <a href="http://127.0.0.1/exam/list.php">List</a>|
<a href="http://127.0.0.1/exam/source_code.php">Source Codes</a> |
Welcome: <strong>
<?PHP
$firstname = getuserinfo('name');
$surname = getuserinfo('surname');
echo "$firstname $surname";
?>
</strong> | <a href="logout.php">Logout</a></h3></td></tr></table>

<h2 style="color: yellow; font-family: Verdana, Arial, Helvetica, sans-serif;" align="center">Members Zone</h2>

<!-- Insert a horizontal line (ruler) -->
<hr /><br />

The problem is the PHP code next to Welcome does not display. Please help.

abduraooft
10-18-2012, 10:05 AM
The problem is the PHP code next to Welcome does not display.
For that, you need to change the extension of that file to PHP. Otherwise, you'd need to configure your server to parse php code in txt file (which is not recommended)

hans_cellc
10-18-2012, 12:06 PM
Got it sorted thanks still in a text file, this is what I did.

For some reason it does echo the SESSION in the PHP code and all I did was to create a SESSION with the name and surname and it works.

Below my code:
header_secure.txt

<h1 style="font-size: 400%; font-family: Forte, Verdana, Arial, Helvetica, sans-serif; color: #000000; margin-top: -7px;" align="center">CONSOLE GAMES FOR SALE</h1>
<h1 style="font-size: 300%; font-family: Forte, Verdana, Arial, Helvetica, sans-serif; color: #FF0000; margin-top: -20px;" align="center">PS2 - PS3 - Wii - X-BOX</h1><br />

<!-- Links -->
<table width="100%" style = "margin-top: -25px;">
<tr align="center"><td ><h3 style = "margin-top: -5px; margin-bottom: 1px;"><a href="http://127.0.0.1/exam/index.php" name="top">Home Page</a> |
<a href="http://127.0.0.1/exam/insert.php">Insert</a> | <a href="http://127.0.0.1/exam/list.php">List</a>|
<a href="http://127.0.0.1/exam/source_code.php">Source Codes</a> | Welcome: <strong><?PHP echo $_SESSION['userlogin']; ?></strong>
| <a href="logout.php">Logout</a></h3></td></tr></table>

<h2 style="color: yellow; font-family: Verdana, Arial, Helvetica, sans-serif;" align="center">Members Zone</h2>

<!-- Insert a horizontal line (ruler) -->
<hr /><br />

alemcherry
10-18-2012, 01:10 PM
First of all, using .txt extension for PHP files is bad practice. Anybody can directly access it via HTTP and see the source code.

Secondly, debugging is a logical process, just like coding. If you dont get something as expected, follow the path and echo some variables and find out the exact reason. Here you could simply print_r($_SESSION) and see what are the variables it is having. Then if needed, echo the $query, verify if it goes into the if condition and print_r the $result and so on. There is no magic, use common sense and logic.

DrDOS
10-18-2012, 10:35 PM
First of all, using .txt extension for PHP files is bad practice. Anybody can directly access it via HTTP and see the source code.

Secondly, debugging is a logical process, just like coding. If you dont get something as expected, follow the path and echo some variables and find out the exact reason. Here you could simply print_r($_SESSION) and see what are the variables it is having. Then if needed, echo the $query, verify if it goes into the if condition and print_r the $result and so on. There is no magic, use common sense and logic.
For someone to access it via HTTP they need to know the name of the file, which is hidden by the php part of the code.

Including as a .txt file is very handy for .css code, menus and other items that can be written verbatim to a page, as that is
what happens with an included .txt file.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum