×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Could someone explain following stuff for me (eval and functions)

Could someone explain following stuff for me (eval and functions)

Could someone explain following stuff for me (eval and functions)

(OP)
Hello people,

I am pretty new to PHP,

I have made following pages


Index.php
contains following code:

CODE --> PHP

<?php

session_start();

$_SESSION['foo']='

<?php
function foo()
{
	echo "asdasdd";

}
?>';

?>
<a href="pag2.php">link </a> 


Below is my code for pag2.php


CODE --> PHP

<?php

session_start();


eval('?>'.$_SESSION['foo']);


foo();

?> 


'eval' does that mean you check if the PHP code is valid?
(I believe you use the 'eval' to check for errors ? correct me if I am wrong)


If I don't want to use the 'eval', just call the function (foo)
how should I write the code instead?


Thank you in advance

RE: Could someone explain following stuff for me (eval and functions)

(OP)
I think I got it though..

I got this code:

index.php

CODE --> PHP

<meta charset="UTF-8">
<?php
include('pag2.php');

getpage();


?> 

and

pag2.php

CODE --> php

<?php


function getpage(){

	echo "this is info from pag2.php";
}

?> 

RE: Could someone explain following stuff for me (eval and functions)


eval('?>'.$_SESSION['foo']);

Means evaluate the text string in the session value named 'foo' and apply it as if if it were a line of php code.

However that IS a rather foolhardy and potentially dangerous (for your server or site) way of using evaluate, simply because it could possibly be used by an attacker to execute arbitrary code in the context of your website user account.

Overuse of eval() in the core code is one of the things that makes WordPress such an easy target for malicious 'hackers', because any sensible and responsible server manager would disable the eval function globally [in php.ini], but WordPress makes this impracticable because then WordPress will not run and requires individual php.ini files to enable it, which negates the security aspect of the global block.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Could someone explain following stuff for me (eval and functions)

elmnas,

Structuring your code is really a "very personal" habit or preference. Take for example:

include
common
js
css
img
bin
data
tmp
class

Each of the above could be directories where you would keep files based on their respective types and/or use within your application. So, you can use include(), require(), require_once() to load the needed documents when needed and in the order they are needed thus basically concatenating (if you will), a PHP script based on needs/requirements.

A nice thing about the use of these approach is that you can set a variable (say $home='1200 SE 32nd Ave.') and upon loading a 2nd script, the variable will be accessible without having to declare it as global.

Do get use to using isset() prior to referencing any variable or object to avoid warnings and/or errors.

One UDF I put together to take advantage of small PHP scripts and get their resolved content upon request is

CODE

function getresolved($page) {
       if(file_exists($page)) {        
        ob_start();
        include $page;
        $content = ob_get_contents();
        ob_end_clean();
        return ($content);
       } else { return(0); }
    } 

Again, all of this is a matter of preference/habit but I hope this gives you an idea of how to structure your project and how to best load/call processing scripts throughout your application.


PS: Credit to above method goes to a member of this very forum (I think vacunita or feherke ...) from many years ago!


--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: Could someone explain following stuff for me (eval and functions)

(OP)
Hi Southbeach@,

I have never thought about if PHP have a certain structure in PHP code intresting :)
I know there is example in dot.net different models you work with (MVC etc)...
Could you provide me with more information if there is a certain way you work with php code?

Thank you in advance.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close