Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts

    POST, fwrite & limiting characters

    I've got something going that auto creates config files for users when they make a new submission to my compatibility list.

    PHP Code:
    $entry = isset($_GET['entry']) ? (int)$_GET['entry'] : 0
    if(isset(
    $_POST['mode'])) { //CONVERT THE HIDDEN MODE SELECTIONS TO OPL SUPPORTED BIT FORMAT
    $anymode = ($_POST['mode']); 
    $patterns = array();
    $patterns[0] = '/1/';
    $patterns[1] = '/2/';
    $patterns[2] = '/3/';
    $patterns[3] = '/4/';
    $patterns[4] = '/5/';
    $patterns[5] = '/6/';
    $patterns[6] = '/7/';
    $patterns[7] = '/8/';
    $replacements = array();
    $replacements[0] = '1';
    $replacements[1] = '2';
    $replacements[2] = '4';
    $replacements[3] = '8';
    $replacements[4] = '16';
    $replacements[5] = '32';
    $replacements[6] = '64';
    $replacements[7] = '128';
    $oplmodes preg_replace($patterns$replacements$anymode);
    $mode_sum 0
    foreach(
    $_POST['mode'] as $mode) { 
        
    $mode_sum += pow(2$mode 1); 
    }  
    //END OF MODE BIT CONVERSION
    //UPDATE CONFIG FILE WITH NEW INFORMATION. HIDDEN INPUTS ON CHILD PAGE FILL IN INFORMATION FROM OTHER PAGES
    //SUCH AS DESCRIPTION, GENRE, ESRB ETC FROM INFO.PHP.
    $usb = ($_POST['usb']." "); //hidden
    $hdd = ($_POST['hdd']." "); //hidden
    $smb = ($_POST['smb'].""); //hidden
    $players = ($_POST['players']);
    $gamename = ($_POST['gamename']);
    $gamecode = ($_POST['gamecode']); //NAME OF CONFIG FILE [hidden]
    $description = ($_POST['synopsis']);
    $developer = ($_POST['developer']);
    $genre = ($_POST['genre']);
    $esrb = ($_POST['esrb']);
    $region = ($_POST['region']);
    $players = ($_POST['players']);
    $releasedate = ($_POST['releasedate']);
    $gamecf $gamecode ".cfg"//BEGIN CREATING OR RE-CREATING CONFIG FILE USING NAME FROM 
                                  //HIDDEN INPUT GAMECODE
    $fh fopen("./cfg/$gamecf"'w') or die("can't open file");
    $oplmode "\$Compatibility=$mode_sum\n\r";
    $description "Description=$description\n\r";
    $compatibility "Compatibility=$usb$hdd$smb\n\r";
    $genre "Genre=$genre\n\r";
    $esrb "Esrb=$esrb\n\r";
    $title "Title=$gamename\n\r";
    $developer "Developer=$developer\n\r";
    $players "Players=$players\n\r";
    $releasedate "Release=$releasedate\n\r";
    $region "Region=$region\n\r";
    $data $oplmode.$title.$genre.$releasedate.$compatibility.$description.$region.$developer.$esrb.$players;
    fwrite($fh,"$data");
    fclose($fh); 
    I need to limit the characters POSTd here to something like 150
    PHP Code:
    $description = ($_POST['synopsis']); 
    Is it possible to limit the number of characters that are POSTd by this input field when the file is written? This field can sometimes contain a lot of characters.. can't be having all of that inside the file.
    Last edited by bemore; 03-01-2013 at 10:55 AM.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Well, why don't you just limit the charachters on the input box or text-box.

    Like this:

    For text-box:

    PHP Code:
    <input type="text" id="synopsis" name="synopsis" maxlength="100" /> 
    But this is not fool-proof. Do server-side checking. Key - Strlen

    PHP Code:
    if(strlen($_POST['synopsis']) > 100)
    {

    //Skip storage to DB and tell user to input less characters in synopsis box



  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Redcoder View Post
    Well, why don't you just limit the charachters on the input box or text-box.

    Like this:

    For text-box:

    PHP Code:
    <input type="text" id="synopsis" name="synopsis" maxlength="100" /> 
    But this is not fool-proof. Do server-side checking. Key - Strlen

    PHP Code:
    if(strlen($_POST['synopsis']) > 100)
    {

    //Skip storage to DB and tell user to input less characters in synopsis box


    Well, the entire input field needs to be sent to database...
    Only when writing to the file do I need it to limit the characters.

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    If you only want 100 characters to be stored to file, then just get the first 100 characters from the string. That is if I understand you right.

    Like:

    PHP Code:
    $stringToBeStoredInFile substr($_POST['synopsis'], 0100); 
    The above will get first 100 characters and store it in $stringToBeStoredInFile variable.

    if I have not adressed your question exactly, post below and be more verbose and clear.

  • Users who have thanked Redcoder for this post:

    bemore (03-01-2013)

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Redcoder View Post
    If you only want 100 characters to be stored to file, then just get the first 100 characters from the string. That is if I understand you right.

    Like:

    PHP Code:
    $stringToBeStoredInFile substr($_POST['synopsis'], 0100); 
    The above will get first 100 characters and store it in $stringToBeStoredInFile variable.

    if I have not adressed your question exactly, post below and be more verbose and clear.
    Yup that's exactly what I was looking for. Thanks a lot!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you don't need to indicate a truncation, than you can simply set your database length as the maximum you will allow (ie: varchar(100)). Anything in excess will be automatically truncated by the dbms. The use of the strlen or substr is most useful if you want to relay to the user that it is being truncated.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    If you don't need to indicate a truncation, than you can simply set your database length as the maximum you will allow (ie: varchar(100)). Anything in excess will be automatically truncated by the dbms. The use of the strlen or substr is most useful if you want to relay to the user that it is being truncated.
    Hmm.. I see there are a few methods for what I was trying to accomplish. I guess using a hidden input to echo the information from the database, and limiting the input's characters. Or the method described above, which is what I used.
    I just needed the whole string to exist in the database, as I use that information for something else also. I needed just a sample of that information to insert into a configuration file that can be downloaded. It seems the answer was fairly simple, and I should have been a bit more specific with my searches before posting a thread.

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    if you want to add those numbers together to create access levels this will pull them apart again:

    PHP Code:
    function breakdown_access($x) {
        
    $n ;
        
    $allow ='-';
        while ( 
    $x ) {
            if ( 
    $x == ) {
              
    $allow .= $n.'-';
            }
            
    $n *= ;
            
    $x >>= ;
        }    
        
    $_SESSION['access_level'] = $allow;
        return 
    $allow;

    and check if there level is good with
    PHP Code:
    function check_access($x) {
        if ((
    strpos($_SESSION['access_level'], $x)) !== false){
            
    $allgood true;
        }
        return 
    $allgood;

    also you can build an array in 1 line to tidy things up:

    PHP Code:
    $patterns = array('/1/','/2/','/3/','/4/','/5/','/6/','/7/','/8/'); 
    Last edited by Arcticwarrio; 03-02-2013 at 10:11 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •