/    Sign up×
Community /Pin to ProfileBookmark

Hello.

Just a new quick query. Hope you’ll be able to help please.

We’ve got quite a few forms on our website, and as they get huge amounts of activity, and we’re using a PHP script to generate a unique ID for each person.

As I didn’t want to use a DB (and still don’t) it’s done using a flatfile.

Basically, I want to now do the following:

  • 1. As you’ll see by the code, the “next ID” is generated whenever the page is simply loaded. I want to change that so an ID is only issued when it’s submitted SUCESSFULLY. I imagine then that the PHP on the form.php itself needs to end up on mailer.php. I’m sure you’ll see what I mean.
    I’ve done what I think I *think* is right (which works), but if you could just confirm? The first pastes are the original, and the last are the newly modified.
  • You’ll also see that in the modified mailer.php (the last paste) I had had change $value (which is on the first form.php (first page)) to $id — as the mailer.php was already using $value — is this OK how I’ve done it?
    2. Any suggestions or modifications?

    form.php:

    [code=php]
    <?php
    include(‘/home/www/htdocs/header.php’);

    $fh = fopen(“/home/www/htdocs/secure/support.txt”,”r”);
    $value = fread($fh, filesize(“/home/www/htdocs/secure/support.txt”));
    fclose($fh);

    $value++;

    $fh = fopen(“/home/home/www/htdocs/secure/support.txt”,”w”);
    fwrite($fh, $value);
    fclose($fh);
    ?>

    <form action=”/mailer.php” method=”post”>
    <p class=”hidden”><input type=”hidden” name=”ID” value=”<?php echo $value ?>” /></p>
    All the form data here (inputs etc…)
    </form>

    <?php
    include(‘/home/www/htdocs/footer.php’);
    ?>
    [/code]

    mailer.php:

    [code=php]
    <?php
    $msg = “”;
    if (empty($_POST[‘NAME’])) {
    $msg .= “Your name cannot be blank.<br />”;
    }
    if (!preg_match(“/^[a-zA-Z0-9]+$/”, $USERNAME)) {
    $msg .= “Your username cannot be blank and must contain only A-Z, a-z and 0-9.<br />”;
    }
    if (!preg_match(‘/[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)*@[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)+/’, $_POST[‘EMAIL’])) {
    $msg .= “Your E-mail address is invalid.<br />”;
    }
    if (empty($_POST[‘COMMENTS’])) {
    $msg .= “Your comments cannot be blank.<br />”;
    }

    if (!empty($msg)) {
    include “/home/www/htdocs/header.php”;
    echo “<p>Errors:</p><div class=”box”><p>$msg</p></div>n”;
    include “/home/www/htdocs/footer.php”;
    exit();
    }

    #######################################################
    # This script is Copyright 2003, Infinity Web Design #
    # Distributed by [url]http://www.webdevfaqs.com[/url] #
    # Written by Ryan Brill – [email][email protected][/email] #
    # All Rights Reserved – Do not remove this notice #
    #######################################################

    $to = “[email protected]”;
    $subject = ‘Support [ID:’ . $_POST[‘ID’].’]’;
    $headers = “From: [email][email protected][/email]”;
    $forward = 0;
    $location = “http://www.ourdomain.com/thankyou/index.php”;

    $date = date (“l, F jS, Y”);
    $time = date (“h:i A”);

    $msg = “Below is the result of your feedback form. It was submitted on $date at $time.nn”;
    $msg .= “IP: “.$_SERVER[‘REMOTE_ADDR’].”nn”;

    if ($_SERVER[‘REQUEST_METHOD’] == “POST”) {
    foreach ($_POST as $key => $value) {
    $msg .= ucfirst ($key) .” : “. $value . “n”;
    }
    }
    else {
    foreach ($_GET as $key => $value) {
    $msg .= ucfirst ($key) .” : “. $value . “n”;
    }
    }

    mail($to, $subject, $msg, $headers);
    if ($forward == 1) {
    header (“Location:$location”);
    }
    else {
    include “/home/www/htdocs/header.php”;
    echo “<p>Thank you!</p>n<p>lots of other text.</p>n<p>Your ID for this submission is: <strong>Support [ID:” . $_POST[‘ID’]. “]</strong>.</p>n”;
    include “/home/www/htdocs/footer.php”;
    }

    ?>
    [/code]

    NEW:

    form.php:

    [code=php]
    <?php
    include(‘/home/www/htdocs/header.php’);
    ?>

    <form action=”/mailer.php” method=”post”>
    All the form data here (inputs etc…)
    </form>

    <?php
    include(‘/home/www/htdocs/footer.php’);
    ?>
    [/code]

    mailer.php:

    [code=php]
    <?php
    $msg = “”;
    if (empty($_POST[‘NAME’])) {
    $msg .= “Your name cannot be blank.<br />”;
    }
    if (!preg_match(“/^[a-zA-Z0-9]+$/”, $USERNAME)) {
    $msg .= “Your username cannot be blank and must contain only A-Z, a-z and 0-9.<br />”;
    }
    if (!preg_match(‘/[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)*@[^x00-x20()<>@,;:\”.[]x7f-xff]+(?:.[^x00-x20()<>@,;:\”.[]x7f-xff]+)+/’, $_POST[‘EMAIL’])) {
    $msg .= “Your E-mail address is invalid.<br />”;
    }
    if (empty($_POST[‘COMMENTS’])) {
    $msg .= “Your comments cannot be blank.<br />”;
    }

    if (!empty($msg)) {
    include “/home/www/htdocs/header.php”;
    echo “<p>Errors:</p><div class=”box”><p>$msg</p></div>n”;
    include “/home/www/htdocs/footer.php”;
    exit();
    }

    $fh = fopen(“/home/www/htdocs/secure/support.txt”,”r”);
    $id = fread($fh, filesize(“/home/www/htdocs/secure/support.txt”));
    fclose($fh);

    $id++;

    $fh = fopen(“/home/home/www/htdocs/secure/support.txt”,”w”);
    fwrite($fh, $id);
    fclose($fh);

    #######################################################
    # This script is Copyright 2003, Infinity Web Design #
    # Distributed by [url]http://www.webdevfaqs.com[/url] #
    # Written by Ryan Brill – [email][email protected][/email] #
    # All Rights Reserved – Do not remove this notice #
    #######################################################

    $to = “[email protected]”;
    $subject = ‘Support [ID:’ . $id .’]’;
    $headers = “From: [email][email protected][/email]”;
    $forward = 0;
    $location = “http://www.ourdomain.com/thankyou/index.php”;

    $date = date (“l, F jS, Y”);
    $time = date (“h:i A”);

    $msg = “Below is the result of your feedback form. It was submitted on $date at $time with ID $id.nn”;
    $msg .= “IP: “.$_SERVER[‘REMOTE_ADDR’].”nn”;

    if ($_SERVER[‘REQUEST_METHOD’] == “POST”) {
    foreach ($_POST as $key => $value) {
    $msg .= ucfirst ($key) .” : “. $value . “n”;
    }
    }
    else {
    foreach ($_GET as $key => $value) {
    $msg .= ucfirst ($key) .” : “. $value . “n”;
    }
    }

    mail($to, $subject, $msg, $headers);
    if ($forward == 1) {
    header (“Location:$location”);
    }
    else {
    include “/home/www/htdocs/header.php”;
    echo “<p>Thank you!</p>n<p>lots of other text.</p>n<p>Your ID for this submission is: <strong>Support [ID:” . $id . “]</strong>.</p>n”;
    include “/home/www/htdocs/footer.php”;
    }

    ?>
    [/code]

    Many thanks in advance!

    Regards,

    to post a comment
    PHP

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @DanUKauthorJul 20.2004 — Hello guys.

    Sorry to be a nuisance.

    Could you just tell me whether what I've done was correct, and whether I was correct to change $value to $id?

    Many thanks in advance...
    ×

    Success!

    Help @DanUK spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...