2

I want to write some information in a txt file (user name, user mail and ip address) and then redirect to another html file. If I try to redirect nothing appears in the text file. But if I don't redirect to an html file the php script is working correctly. What am I missing?

<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt1 = file_get_contents("newfile.txt");
$ip = get_client_ip_server();
$txt2 = $_COOKIE["userName"] ."\t" .$_COOKIE["userEmail"]." \t".$ip. "\r\n";
$txt3 = $txt1 . $txt2;
fwrite($myfile, $txt3);
fclose($myfile);
die();
header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance
header( "Location: general.html" . $_SERVER["REQUEST_URI"] );
// Function to get the client ip address
function get_client_ip_server() {
$ipaddress = '';
      if (getenv('HTTP_CLIENT_IP'))
            $ipaddress =getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
}
?>

Thanks for your help

Idrees Samim
  • 463
  • 5
  • 15

2 Answers2

5

Ok someone posted an answer, so I will also.

die(); is before header so header never happens. Put the die after header.

References:


Edit:

After a successful test of your code and my suggestion to move die(); after header; make sure that the cookies are indeed set/not empty and you (may) need to remove $_SERVER["REQUEST_URI"] from header( "Location: general.html" . $_SERVER["REQUEST_URI"] );

Using header( "Location: general.html" . $_SERVER["REQUEST_URI"] ); failed for me to redirect to general.html and removing $_SERVER["REQUEST_URI"] did redirect to general.html.

References:


Using this as a test:

<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt1 = file_get_contents("newfile.txt");
$ip = get_client_ip_server();
// $txt2 = $_COOKIE["userName"] ."\t" .$_COOKIE["userEmail"]." \t".$ip. "\r\n";

$txt2 = "Username cookie"  ."\t" . " Email cookie " ." \t".$ip. "\r\n";

$txt3 = $txt1 . $txt2;
fwrite($myfile, $txt3);
fclose($myfile);

header( "HTTP/1.0 302 Found" );
header( "Status: 302" ); # this is for chrome compliance

header( "Location: general.html");
// header( "Location: general.html" . $_SERVER["REQUEST_URI"] );

die();

// Function to get the client ip address
function get_client_ip_server() {
$ipaddress = '';
      if (getenv('HTTP_CLIENT_IP'))
            $ipaddress =getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
}
?>

Yielded: (numbers are examples only)

Username cookie  Email cookie   11.22.33.44

Username cookie  Email cookie   11.22.33.44

Username cookie  Email cookie   11.22.33.44

About $_SERVER["REQUEST_URI"]

'REQUEST_URI' The URI which was given in order to access this page; for instance, '/index.html'.

See also:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Edit #2:

After testing something I think is preventing your redirection, is that your file may have been saved as UTF-8 with BOM (byte order mark) and if this is the case, then you are outputting before header. Error reporting will notify you of that.

Either you save that file as ANSI, or UTF-8 without BOM.

Consult:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

The die is before the redirect, you should put it after the header redirection ;)