0

UPDATE: Thank you all for the late night help. I Have updated the code below with the changes that I have made. If a line is commented it is something I tried and it didn't work, what isn't commented out right now was my last attempt. I am still not able to get my page to load. Again Thank You!.

Website location: www.encapdio.com/pyscraper.php I am a systems guy taking a shot at programming and decided to put a website together to learn php. My code if causing the page to error 500 and even with my php.ini file showing errors it seems its just breaks. I can't find any errors in my code, but I most likely need a fresh pair of eyes. This is what it should do.

The purpose of this page is to grab line per line stock ticker symbols. Once you hit submit my code should check the textarea if its empty. If it isn't empty then take the $content in to an array, add $linebreak to the end of each array item and account for \r type line breaks, Next write each entry line by line in to the file. If it is empty to echo out that no tickers were entered.

Then there if all was successful and the status = results you get to see the ticker symbols next to their stock price. I have a fully tested python script checking the ticker_inputs.txt file that will execute my webcrawler/scraper/ticker_inputs.txt updater.

It is the php that I cannot get to update the ticker_input.txt file or even show the php and html on the same page without that server error 500.

Example Code:

<?php
include("inc/header.php");
include("inc/nav.php");

#if (isset ($_POST[$text_name])) && $_POST['text_name'] != "") {
if (!empty($_POST['textfile'])) {
    $content = isset($_POST['textfile'])?$_POST['textfile']:"";
    $linebreak = explode("\n", str_replace("\r", "", $content));
  $tick_wfile = fopen("ticker_input.txt", "w") or die("Unable to open file!");
    fwrite($tick_wfile,$linebreak);
    fclose($tick_wfile);
   if (empty($_POST['textfile'])) {
   echo "No Tickers Were Entered!";
  }
header("Location: pyscraper.php?status=results");
exit;
}
include("inc/header.php");
include("inc/nav.php"); ?>

<div class="container" align="center">
    <div class="page-header">
          <br><h1>Web Scraper<small> built with python</small></h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "results") {
         $tick_rfile = fopen("ticker_input.txt","r") or die("Unable to open file after writting to it!");
        echo fread($tick_rfile,filesize("ticker_input.txt"));
        fclose($tick_rfile);
} else { ?>
    <form name="savefile" method="post" action="">
        <fieldset class="form-group">
        <label for="tickerlist">Enter Tickers <small>(one per line)</small></label>
        <textarea name="textfile" class="form-control" rows="5"></textarea>
        </fieldset>
        <button type="submit" name="submit_file" value="Save File" class="btn btn-primary">Submit</button>
    </form>
    <?php } ?>
    </div>
</div>
<?php include("inc/footer.php"); ?>
Edwin Carra
  • 97
  • 1
  • 7

4 Answers4

0

Try changing:

header("location:pyscraper.php?status=results");

To

header("Location: pyscraper.php?status=results");
exit;

Basically, without the exit; the rest of the PHP is still executed. Also HTTP headers are case sensitive.

If this does not work, I'll remove the answer. Posting as a comment would have been harder to follow.

Community
  • 1
  • 1
Tigger
  • 8,307
  • 4
  • 32
  • 38
0

One closing parenthesis is missing in this line

$linebreak = explode("\n", str_replace("\r", "", $content);

Check your code

Haridarshan
  • 995
  • 14
  • 32
  • That should have produced a different error, but well spotted. – Tigger Feb 10 '16 at 06:01
  • 1
    No, apache will throw 500 error in that case and php will write the correct error in error_log file – Haridarshan Feb 10 '16 at 06:01
  • On FreeBSD with Apache 2.4.x I get a `Parse error: syntax error, unexpected ';' in ...` error. – Tigger Feb 10 '16 at 06:05
  • By default, `display_errors` is off, so in that case apache throws 500 error and log gets written to either `syslog` or `error_log` file. To display the error, we need to turn on the `display_errors` in `php.ini`. After that only we can see the exact error on the browser. – Haridarshan Feb 10 '16 at 06:24
  • Actually, the `display_errors` default is '1', which is "on" according to the [PHP Docs](http://php.net/manual/en/errorfunc.configuration.php). – Tigger Feb 10 '16 at 06:38
0

An open paranthesis is missing on fourth line after isset.

Also you can't check using isset on a function call.Try using empty() instead. $_POST['textfile'] is always set but can be empty

check this link for more

Community
  • 1
  • 1
jayadevkv
  • 376
  • 3
  • 16
  • I am not using empty, but still no change. Found it was my if statements required '()' around the empty function. I got the page back up, but the code isn't updating my ticker_input.txt file – Edwin Carra Feb 10 '16 at 16:46
0

You are missing the isset brackets here if (isset $_POST[$text_name]). Try this instead to check if its empty and if it's set if(isset($_POST[$text_name]) && $_POST['text_name'] != "")

Rusty
  • 7,112
  • 10
  • 46
  • 69