-2

I have the following code:

<?php
$email = $_POST['email'];
if ($email!=='ThisMail'){
header ('Location:http://www.mywebsite.com/');
$handle = fopen("data.txt", "a");
foreach($_POST as $variable => $value) {
   fwrite($handle, $variable);
   fwrite($handle, "=");
   fwrite($handle, $value);
   fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
}
else{
header ('Location:http://www.google.com/');
exit;
}
?> 

I am basically collecting information from a form on my website, with action=form.php (where this code is saved). I want it so that if the email field on my HTML document where the form is is filled out with a particular email address, ThisMail, it will write all the stored data from the form in the file data.txt and redirect to the webpage I want.

The other side of the if statement simply redirects the user to another page.

This does not seem to be working for me. How can I fix this?

Eux Luxed
  • 11
  • 2
  • What are you trying to do? I don't get it. – Andreas Jul 22 '17 at 13:53
  • You might want to check for equality (`===`), not inequality (`!==`). Also note that this check is case sensitive. Consider using [strcasecmp](http://php.net/manual/en/function.strcasecmp.php). – ccKep Jul 22 '17 at 13:54
  • @Andreas basically take an email input from the user, and if it is a bad email, i.e admin@mywebsite.com then return them to this page, but if it isn't a bad email, i.e !=='theemail@mywebsite.com' then save the email and all other variables from the form on a text document, and redirect them to another page. Isn't very practical I know, but is useful for me. – Eux Luxed Jul 22 '17 at 13:55
  • @ccKep I don't think that is a problem, as when I run the script it just opens a blank page that is the php file and shows nothing instead of going to the correct web page. – Eux Luxed Jul 22 '17 at 13:56
  • I'm assuming you have some code before that snippet, eg. actually declaring `$email` ? – ccKep Jul 22 '17 at 13:59
  • 1
    where and how is `$email` defined and its actual value? the question's unclear. – Funk Forty Niner Jul 22 '17 at 14:01
  • @ccKep as someone suggested in the answers, and my apologies for neglecting to add this, I have this $email = $_POST['email']; just after – Eux Luxed Jul 22 '17 at 14:02
  • the only possible solution I can provide for what was posted, is to use PHP's error reporting http://php.net/manual/en/function.error-reporting.php in order to check for possible errors. Also to make sure that the file has proper permissions to be written to. @EuxLuxed best I can offer for you here. – Funk Forty Niner Jul 22 '17 at 14:07
  • so @EuxLuxed any update as to what's (not) going on? – Funk Forty Niner Jul 22 '17 at 14:17

1 Answers1

0

Here, $email is not defined, so you should add this before the if statement:

$email = $_POST['email']; // here 'email' is the field name in your HTML page
thchp
  • 2,013
  • 1
  • 18
  • 33
  • 1
    Theoretically correct, but we don't know what the origin of that variable is. They might even have left it out from the question. – Funk Forty Niner Jul 22 '17 at 14:02
  • Apologies for neglecting to add this to my question, I already have this but it still does not appear to be working, I do not get redirected. – Eux Luxed Jul 22 '17 at 14:02