Jump to content


  • Posts

  • Joined

  • Last visited


Posts posted by etrader

  1. I use a simple php code (taken from php.net example) to resize an image


    list($width, $height) = getimagesize($filename);
    $new_height = $height / $width * 400;
    $image_p = imagecreatetruecolor(400, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height);
    imagejpeg($image_p, "new.jpg");


    Here, we create a blank image and merge it with the resized image. I wonder if it is the simplest way to resize and save an image. Is it really necessary to create a blank background image?

  2. I have a normalized database in the form of


    ID   user_id     key           value
    1    3           first_name    Brian
    2    3           last_name     Tardy
    3    4           first_name    John
    4    4           last_name     Wilson



    What is the fastest way to capture these values within php? I want to perform a SELECT to catch the values for every user as


    $result=mysql_query("SELECT * FROM users_data WHERE user_id = '$id'");
    while($row=mysql_fetch_array($result)) {
    here we process all data for a given user
    and write it to text file as a single line


    Since it is a long list (100K users with about 10M of rows in user_data), I am looking for the fastest and most reliable way to scan all the database.


  3. I've read lots of articles and discussions about security issues for using session for user authentication; but I did not come to an explicit conclusion which is the best method.


    I decide to generate two session parameters (1) ID (2) a randomly generated Access Token which is stored in the database. I want to add more security:


    1. Where to store session? server-side or client-side by session session.use_only_cookies ?


    2. Is it good to check IP and User Agent before authentication by session? In this case I will lose the option of Remember Me as IP can be changed.


    3. Which of the session parameters of http://php.net/manual/en/session.configuration.php is more practical to have a good security?



    Any additional idea?

  4. 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?



  5. All instructions for enabling php-dba is via fresh installation of php with --enable-dba=shared ....

    When php has been installed, one can install dba later by yum install php-dba. In this method there is no place to define the location of each database dir, as we need to enable some of them due to possible conflicts.


    How to install php-dba via yum?

  6. Is there something like str_pad to make a string with a fixed width on the bases of bytes (not characters). If not, is it possible to safely convert characters to bytes to have a fixed width (of bytes). As a matter of fact, I want to use the strings (to be written to a file) as offsets for reading lines with fseek.


    Thanks :)

  7. I have want to install dBase on my servers (CentOS and Ubuntu), but the problem is that PHP 5.3.6 has been installed by yum as php53. dBase is available in repositories as php-dbase, and this will have conflict with php-common.


    Alternatively, I tried to compile it from source, but after downloading php 5.3.8 (the latest version), I just found that dbase does not exist in ext folder.


    How can I enable dBase on php 5.3.6?

  8. I want to load and display the content of a text file (actually it is a cache file). I tested both method using a long for loop as


    for ($i=0; $i < 10000; $i++) {
    $handle = fopen("test.txt", "r");
    $text = fread($handle, filesize("test.txt"));
    echo $text;


    for ($i=0; $i < 10000; $i++) {
    $text = file_get_contents("test.txt");
    echo $text;


    but the results were similar, though with a large distribution of the comparative data. The speed of a method was 0.5 to 2.0 faster than the other one (in various runs). Which method do you recommend? Could you please quote the pros and cons of each method?

  9. I have a large number of files. It is recommended to save the files with hashing system to have fast access to files through the OS. But I have no practical knowledge about hash. Could you please give me a hint, how to save a file with hash coded system via php? and how to read the hash coded filename by php?


    Thank you in advance!

  10. I want to parse some tiny lines from a webpage. I have few questions:


    1. What is faster and better: cURL or file_get_contents?

    2. Is it better to run preg_match commands form every line I want OR first get a shorter part (e.g. between <head> tag) then running preg_match for the lines?

    3. Actually, I need to load the whole page before parsing. Is there a way to stop loading the whole page. For example, when I want something between <head> tag; stop loading <body>?


    Thank you in advance!

  11. I have a function to read terms from an array stored in language file as

    function _language($term) {
    if(!empty($lang[$term])) {
    } else { $lterm = $term;}
    return $lterm;


    The problem is that I get the language code from url as ?lang=en or ?lang=fr; but I cannot bring this string into the function. How I can tell the function to read appropriate language file (fr.php or en.php or es.php) upon choosing language in ?lang=XX

  12. Here, it is


    function duration($amount){
               $h =  $amount / 3600 ;
               $h =  floor($h) ;
               $min = $h * 3600 ;          
               $min = $amount - $min ;
               $min =  $min / 60 ;
               $min =  floor($min) ;
               $sec = ($min * 60) + ($h * 3600);          
               $sec = $amount - $sec ;
               $return = "$h:$min:$sec" ;
               return $return ;
    $second = 7291 ;
    $time = duration($second) ;
    echo $time ;

  13. I want to update a value by setting cookie. I use this code:


        if (isset($_POST['ChangeOrdering'])) {
            setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000);
    echo $_COOKIE["Ordering"];
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>"> Reorder messages:
    <select name="ChangeOrdering">
    <option value="DateAdded ASC">Oldest first</option>
    <option value="DateAdded DESC">Newest first</option>
    <option value="Title ASC">By Title, A-Z</option>
    <option value="Title DESC">By Title, Z-A</option>
    <input type="submit" value=" Save Settings " />


    Problem is that echo will return the previous cookie set, not the last value. As a matter of fact, my problem is about updating the cookie. It seems that the browser load the cookie before it is updated by the POST value.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.