Using fopen, fputs, fclose

Discussion in 'PHP' started by fex, Aug 19, 2008.

  1. #1
    Hello,

    I added a poll system to my website and in order to add a poll you need to add several lines in the config.php file to set the poll, like this:

    $p = new Poll;
    $p->question = "Fav color?";	
    $p->returnToURL = "../poll.php";				
    $p->legend = "Test Poll";						
    $p->control_type = $CONTROL_RADIOBUTTONS;		
    $p->add_value("1", "Green");					
    $p->add_value("2", "Blue");
    $p->add_value("3", "Yellow");
    $VALID_POLLS["1"] = $p;
    Code (markup):
    I already have an admin area on my website and I want to be able to add polls directly from my webpage, so without opening my config.php in expression web. That's why I was trying to do this with fopen, fputs and fclose.
    Here's what I've got so far:

    $att1 = "../../poll/data.php"; 
                              $att2 = fopen ($att1, "rb"); 
                              $currententries = fread ($att2, filesize ($att1)); 
                              fclose ($att2); 
     					      // New poll
                              $fp = fopen("../../poll/data.php",  "w+");   
                              fputs($fp, "$p = new Poll;" . "$p->question = " . $question . ";" . "$p->returnToURL = '../poll.php';" . "$p->legend = " . $legend . ";" . "$p->control_type = " . $control_type . ";" . "" . "$VALID_POLLS[" . $poll_id . "] = $p;" . $currententries); 
                              fclose($fp);
    Code (markup):
    I'm guessing there's an error in it, but I can't find it.. Because when clicking on the button which submits the form (with poll information) to the page (insert.php) in which the code above is included, my web browser tells me there's an 'internal server error' or 'HTTP-error 500'.

    As newbie in php I'm not quite aware of what that means but It can't be good, that's for sure.. :D

    Hope someone is willing to help me out,,
     
    fex, Aug 19, 2008 IP
  2. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your code looks okay. Error 500 is an internal server error. It's a very general error which usually means the server can't be more specific, so your error could be being caused by one of many things. You say this is your "admin area". Is your admin area a folder that contains an .htaccess file? If so, try renaming the file to anything else. See if you still get your error.
     
    jack_ss, Aug 19, 2008 IP
  3. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It might be a file permissions issue (although usually that would give you a different error). Try chmoding the data.php file and all the folders in its path to 777 and see if that fixes the problem (if you're worried about security, you can change the permissions to something safer once you verify whether or not it's a permissions issue).
     
    Shoro, Aug 19, 2008 IP
  4. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It seems the following piece of code is causing the error:
    "$VALID_POLLS[" . $poll_id . "] = $p;"

    The variable $poll_id is a numeric value, here's how I defined it:
    $poll_id = $HTTP_POST_VARS['pollid'];

    I know defining a numeric value ($var = number) is different than defining a string ($var = "text";), but writing $poll_id = $HTTP_POST_VARS[pollid] wouldn't work.
    I need to change "$VALID_POLLS[" . $poll_id . "] = $p;"into "$VALID_POLLS[$poll_id] = $p;" ??
     
    fex, Aug 20, 2008 IP
  5. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've found what was causing it not to work. The " " signs in the fputs had to be replaced by ' ' signs. Because each time it inserted $p (which appears in each line of the code) it treated that like a <p> tag, which caused my php code to be converted into regular html code,, Here's what I've changed:

    - String variable definitions: also replace " " by ' '. (ex: $control_type = '$CONTROL_RADIOBUTTONS'; )
    - The fputs command:
    fputs($fp, '<? $p = new Poll;' . '$p->question = ' . '"' . $question . '"' . ';' . '$p->returnToURL = ' . '"' . $url . '"' . ';' . '$p->legend = ' . '"' . $legend . '"' . ';' . '$p->control_type = ' . $control_type . ';' . '$VALID_POLLS[' . '"' . $poll_id . '"' . '] = $p; ?>' . $currententries);
    Code (markup):
    I'm now able to get all the information of my form into the data.php file which I include in the config.php file. Now all I need to do is finding a way to actually add the poll to the page by adding these lines to the webpage:

    			<p class="header">Poll Name</p> //Needs to become a variable
    			<div id="poll">
    			<?php 
    				include_once('poll/poll.php');
    				show_vote_control('pollid'); //Needs to become a variable
    			?>
    			</div>
    Code (markup):
    I guess it can be done on a similar way,,
     
    fex, Aug 20, 2008 IP