TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-11-2009, 09:29 AM   #1 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default $content = include('file2include.php');

Well I tried something funny some days ago - include a file with include and put it in a variable

file2include.php
PHP Code:
<table>
<tr><td><?php echo 'hello world' ?></td></tr>
</table>
now I include that file like:
PHP Code:
<?php
$content 
= include('file2include.php');

echo 
$content;
?>

Now what I get in the Browser is:

HTML Code:
<table>
<tr><td>Hello World</td></tr>
</table>1
Now I wondered where that 1 comes from? Is it a true which comes since the file was successfully included - and get's converted to a 1?

(This thread is not about - if we should use something like that above or not )
Sakakuchi is offline  
Reply With Quote
Old 06-11-2009, 09:49 AM   #2 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

include comes back as a boolean
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 06-11-2009, 10:00 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

If the file has been included successfully, and there is no return statement in the included file, $content will be an integer 1. If the file returned something, then $content will be that value. If the file could not be included for whatever reason, then $content would be a boolean FALSE and an E_WARNING would be issued.
Salathe is offline  
Reply With Quote
Old 06-12-2009, 11:41 AM   #4 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

lol, sorry for an dumb question like that, but I got a code like that

PHP Code:
$content = include('pages/ok_cart_product.php');
$content  substr($content,0,-1);
echo 
$content
So I thought the one was added at the end of $content -since he cutted the last character away

changed it now to
PHP Code:
include('pages/ok_cart_product.php'); 
since there is no need of $content | THX for clearing that up
Sakakuchi is offline  
Reply With Quote
Old 06-12-2009, 03:33 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

In a more learning purpose.. How would you get the content of a PHP file(with the PHP intact), and assign in to a variable? Is that even possible?
__________________
Tanax is offline  
Reply With Quote
Old 06-12-2009, 03:39 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
In a more learning purpose.. How would you get the content of a PHP file(with the PHP intact), and assign in to a variable? Is that even possible?
Same way you would any text file, fopen.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-12-2009, 03:39 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It is possible with file_get_contents and eval if you wish to execute the code, too. If nobody else has posted the code for you by the time I get my laptop back, I will write an example !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-12-2009, 07:55 PM   #8 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

No idea how wildhoney wants to go with eval - but you can do it like:

PHP Code:
<?php
ob_start
();
include(
'file2include.php');
$var ob_get_contents();
ob_end_clean();
?>

<html>
<head></head>
<body><?=$var ?></body>
</html>
and the file which gets included:

PHP Code:
<table>
  <tr>
    <th>Column 1 Heading</th>
    <th>Column 2 Heading</th>
  </tr>
  <tr>
    <td><?='content1' ?></td>
    <td><?='content2' ?></td>
  </tr>
</table>
Would give that output:

HTML Code:
<html>
<head></head>
<body><table>
  <tr>
    <th>Column 1 Heading</th>
    <th>Column 2 Heading</th>
  </tr>
  <tr>
    <td>content1</td>

    <td>content2</td>
  </tr>
</table>
</body>
</html>


Edit: sorry forgot to explain what happens actually:

PHP Code:
<?php
ob_start
(); //This is starting the buffer
include('file2include.php');//now we load the file into the buffer
$var ob_get_contents();// load the buffer to $var
ob_end_clean();//delete the buffer
//and thats it ;-)
?>

Last edited by Sakakuchi : 06-12-2009 at 07:58 PM. Reason: forgot to add description
Sakakuchi is offline  
Reply With Quote
Old 06-12-2009, 09:42 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

ARGH, just read the post above sigh

Long day.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-12-2009 at 09:44 PM. Reason: im blind!
sketchMedia is offline  
Reply With Quote
Old 06-15-2009, 07:15 AM   #10 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

@Saka why would you do all that?
I usually just say include (...)
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-16-2009, 05:52 AM   #11 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Well as I said it was not my code -and I changed it later on to just "include" - but you could use for something like:

We want to build an small login widget. The html code is somewhere in an extra html file, and we are currently in a class which builds the sidebar widgets.

Now we dont want to "echo" the code when it is executed since we still have some other html which goes first (<head></head><body> etc.). So we could go, copy the content in our variable - and include it anywhere in our site.

Well, it might not be the best example - but I cannot think of any better atm
Sakakuchi is offline  
Reply With Quote
Old 06-16-2009, 06:53 AM   #12 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

That lost me...
If you had a function that spit out a your login/join box, you would say:
PHP Code:
<?php
 display_login
();
?>
// display_login would be a div with a form. Right?
Personally I would have a file called loginbox.php with my form code, then if I needed that my code would be:
PHP Code:
<h1>Welcome please log in:</h1>
<?php include_once('loginbox.php'); ?>
If I did the function, I'd just have it <<<EOF out, then do my code...
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design