penguin0 Posted May 31, 2007 Share Posted May 31, 2007 I am having a problem sending a small php code through a mysql database. I am trying a simple echo command like <? echo "$usersname"; ?> inside of the html that is saved in my database. I call the content for each page like this: <? $result = mysql_query( "SELECT content FROM pages WHERE number = 1" ); $num_rows = mysql_num_rows( $result ); while ( $a_row = mysql_fetch_row( $result ) ) { foreach ( $a_row as $field ) print "$field"; } ?> and above that I call all the users info like this: $result = mysql_query( "SELECT * FROM users WHERE session = '$userssession'" ); $num_rows = mysql_num_rows( $result ); while ( $a_row = mysql_fetch_array( $result ) ) { $usersid = $a_row['id']; $usersname = $a_row['name']; $usersposition = $a_row['position']; $usersusername = $a_row['username']; $usersemail = $a_row['email']; $userscreated = $a_row['created']; $usersidle = $a_row['idle']; $online = $a_row['online']; $admin = $a_row['admin']; $pageman = $a_row['pageman']; $userman = $a_row['userman']; $rateman = $a_row['rateman']; $menuman = $a_row['menuman']; $users = $a_row['users']; } Is there something wrong with how I call the html ($content) or can this not be done? When I hard code this <? echo "$usersname"; ?> into a php page it works. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/ Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 www.php.net/eval Be careful with that though, it can be dangerous. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-265862 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 Is there a way to make it more secure? This will only be used for people with page admin permissions, after they log in. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266331 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Not that I know of, it is hard to filter out "dangerous" code due to the fact that anyone can really exploit the code, say and admin gets mad at you he just has to write one that runs a query to delete all the databases etc. Or he runs one that uses the eval after fetching a script from the web that installs or something like that. Just not good practice using that eval. Very very dangerous. Now you could setup a system that does not allow that code to be ran unless reviewed by you. And also set it up to filter out mysql_query's and eval statements and include statements. But yea. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266333 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 ok, I did this: $sql = "SELECT content FROM pages WHERE number = 1"; $result = mysql_query($sql, $link) or die(mysql_error()); while ( $row = mysql_fetch_array( $result ) ) { $pname = $row['name']; $content = $row['content']; } eval("$content = \"$content';\"); echo $content; well I am getting this error: Parse error: syntax error, unexpected '<' in path/to/index.php(12) : eval()'d code on line 1 that is comming from the html i store in the db under $content Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266343 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 eval("$content = \"$content\";\"); Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266360 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 Actually what I used was eval("$content = \"$content\";"); and I still get the error. $content is HTML so there will be < and > in the tags, is that causing the problem? Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266395 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 eval('$content = \"' . $content . '\";'); Try that. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266411 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 now I get: Warning: Unexpected character in input: '\' (ASCII=92) state=1 in path/to/romac/index.php(12) : eval()'d code on line 1 Parse error: syntax error, unexpected T_STRING in path/to/domains/translucent-ro.com/public_html/romac/index.php(12) : eval()'d code on line 1 Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266418 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 eval('$content = "' . $content . '";'); forgot with ' you do not need to escape " Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266458 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 Parse error: syntax error, unexpected T_STRING in /path/to/index.php(12) : eval()'d code on line 1 Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266497 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 What does $content contain? Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266499 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 It's just HTML content, like tds, trs, and lots of "". Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266503 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 try this: eval('$content = "' . addslashes($content) . '";'); To escape the lots of " " Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266506 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 Ok that got rid of the error, but the php wont work: to display the page: $sql = "SELECT content, name FROM pages WHERE number = 1"; $result = mysql_query($sql, $link) or die(mysql_error()); while ( $row = mysql_fetch_array( $result ) ) { $pname = $row['name']; $content = $row['content']; } eval('$content = "' . addslashes($content) . '";'); echo $content; The php I want to eval <? echo "$pname"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266510 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 I do not think eval's scope reachs out to the page running the eval. IE if $pname is in $content you should probably do something like this: <?php $sql = "SELECT content,name FROM pages WHERE number = 1"; $result = mysql_query($sql, $link) or die(mysql_error()); while ( $row = mysql_fetch_array( $result ) ) { $pname = $row['name']; $content = $row['content']; $content = str_replace('$pname', $pname, $content); } eval('$content = "' . addslashes($content) . '";'); echo $content; ?> the code executed inside the eval is like doing it inside a function, inside the function all that is known to it are what has been defined in that scope. I am not 100% sure that is why, but I am pretty sure. $pname was not defined in $content, there for it would just print nothing to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266513 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 The only problem doing it that way is, I wanted any php in $content to print, so I could have some variables (not many) come through with the html, so everything is not hard coded. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266514 Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Author Share Posted June 1, 2007 I actually did that and it wont print: $sql = "SELECT content, name FROM pages WHERE number = 1"; $result = mysql_query($sql, $link) or die(mysql_error()); while ( $row = mysql_fetch_array( $result ) ) { $pname = $row['name']; $content = $row['content']; $content = str_replace('$pname', $pname, $content); } eval('$content = "' . addslashes($content) . '";'); echo $content; Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266542 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Dunno man, I never worked with eval too much the best I can do is say read the user comments at www.php.net/eval other than that it's like I said before, you are probably better off not using it and finding a different way. Quote Link to comment https://forums.phpfreaks.com/topic/53784-storing-php-in-a-database-to-then-call-it-with-a-variable/#findComment-266553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.