etrader Posted October 30, 2011 Share Posted October 30, 2011 I produce a series of php variables in a php strict (mostly come from mysql), and I want to save them as a php file. To be use for "include". What is the safest way to write them in a php file? I tried to save them in the manner of saving a text file with fwrite as $data='<?php $cat=array('array created in the text'); $string='some value';?>'; $fh = fopen("file.php", 'w'); fwrite($fh, $data); Now, file.php is a normal php file and I can call it with "include"; but the problem is that this method is not safe. For example, if the string is "There's something", php gives error for the presence of ' in the string. I can skip ' and " by backslash; but this method is costy, as I need to perform this process for all strings to be saved. Moreover, there might be other source of errors too (that I have not encountered yet). Is there a safe way for writing a php code to file? Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/ Share on other sites More sharing options...
tqla Posted October 30, 2011 Share Posted October 30, 2011 Try using addslashes() Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/#findComment-1283393 Share on other sites More sharing options...
trq Posted October 30, 2011 Share Posted October 30, 2011 The idea of creating php files on the fly wreaks of bad design. Can you explain in detail why you would be doing this? Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/#findComment-1283412 Share on other sites More sharing options...
etrader Posted October 31, 2011 Author Share Posted October 31, 2011 I want to make a local version of some variable taken from database to reduce the number of mysql queries. For example, creating a local menu as an array in php file instead of making a query every time to take list of menu from mysql database. Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/#findComment-1283578 Share on other sites More sharing options...
mika Posted October 31, 2011 Share Posted October 31, 2011 You can make an additional PHP script directory outside the web server's document root. Make sure the directory is writable by the web server. This way the files are always accessed by the web server but not by the web browser directly. Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/#findComment-1283671 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2011 Share Posted October 31, 2011 The mysql query cache will cache the result for you - The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client. The query cache can be useful in an environment where you have tables that do not change very often and for which the server receives many identical queries. This is a typical situation for many Web servers that generate many dynamic pages based on database content. The query cache does not return stale data. When tables are modified, any relevant entries in the query cache are flushed. Quote Link to comment https://forums.phpfreaks.com/topic/250088-how-to-safely-write-a-php-file/#findComment-1283675 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.