0

I have a webform with basic info (Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message)

After submitting the form, and inserting the data of those fields into a MySQL database, I want to be able to be redirected to a different URL.
I tried using the header function for PHP but for some reason it will not redirect me to the URL I specified.

    <?php
ob_start();
require("config.php");
include 'config.php';
ini_set('display_errors', 0);
if($_GET['action'] == 'check')
{
   if($_POST['Fname'] == '' || $_POST['Lname'] == '' || $_POST['Uname'] == '' || $_POST['Email'] == '' || $_POST['Phumber'] == '' || $_POST['Age'] == '' || $_POST['Facebook'] == '' || $_POST['Major'] == '' || $_POST['Education'] == '' || $_POST['Music'] == '' || $_POST['Fav'] == '' || $_POST['message'] == '')
   {
      exit;
   }
   else
   {
if($_POST['Email'] !== $_POST['Email'])
         {
         exit;
         }
      else
         {
         $result = mysql_query("SELECT * FROM loginphp
         WHERE Uname='{$_POST['Uname']}'") or die(mysql_error()); 
         $row = mysql_fetch_array( $result );
         if($row['Uname'] == '')
            {
            $_Fname = str_replace("<", "", $_POST['Fname']);
            $_Lname = str_replace("<", "", $_POST['Lname']);
            $_Uname = str_replace("<", "", $_POST['Uname']);
            $_Email = str_replace("<", "", $_POST['Email']);
$_Phumber = str_replace("<", "", $_POST['Phumber']);
$_Age = str_replace("<", "", $_POST['Age']);
$_Facebook = str_replace("<", "", $_POST['Facebook']);
$_Major = str_replace("<", "", $_POST['Major']);
$_Education = str_replace("<", "", $_POST['Education']);
$_Music = str_replace("<", "", $_POST['Music']);
$_Fav = str_replace("<", "", $_POST['Fav']);
$_message = str_replace("<", "", $_POST['message']);
            mysql_query("INSERT INTO loginphp 
            (Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message) VALUES('$_Fname', '$_Lname', '$_Uname', '$_Email', '$_Phumber', '$_Age', '$_Facebook', '$_Major', '$_Education', '$_Music', '$_Fav', '$_message') ") 
            or die(mysql_error());
header("Location: main.php");
exit();
    }
}
}   
}
?>

Warning: Cannot modify header information - headers already sent by (output started at register.php:1) in register.php on line 47

-------------- The answer

This problem doesn't want to get solved so I add print with html code to redirect the page after insert the data to solve it

      mysql_query("INSERT INTO loginphp 
            (Fname, Lname, Uname, Email, Phumber, Age, Facebook, Major, Education, Music, Fav, message) VALUES('$_Fname', '$_Lname', '$_Uname', '$_Email', '$_Phumber', '$_Age', '$_Facebook', '$_Major', '$_Education', '$_Music', '$_Fav', '$_message') ") 
            or die(mysql_error());
echo '<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://www.http://stackoverflow.com/">';
exit();

Thanks for everyone tried to help !

2 Answers2

0

Remove all output before header like echo or print

And use exit after header.

 header("Location: main.php");
 exit();

Update

If you turn on output buffering you can output in the script but PHP doesn't have to send the headers until the buffer is flushed. ... Check something with echo , print() or printr() in the file. It might be that this is the problem OR if any file, then check the number of spaces.

  $form = "<br>";
  //show the form
  $form .="<center>";
  $form .= '<form method .....';

...... ......

And write this at last line before ?>

   echo $form;
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

You must remove all of echo, print_r and var_dump functions before changing header. In your case, if you don't have echo like functions in config.php file, you can try this one:

//ini_set('display_errors', 1); commented in editing
ob_start();
//include the header
require("config.php");
if($_GET['action'] == 'check'){
//your code
}
//other codes (echo and print and vardump)

It's better to use template engines like Twig

x01saa
  • 460
  • 1
  • 8
  • 18
  • I tried what you told me, but the page didn't even show up ! also I don't have any print .. etc inside the file config.php – user7420580 Jan 15 '17 at 08:14
  • possibly you have BOM character (invisible) in first of file. remove it with `awk` or `Notepadd++`. – x01saa Jan 15 '17 at 08:28
  • Yes I just checked the first file and I notiected that I had Bom character inside it. and I removed them. But the problem didn't fix. Do you want to contact me on skype ? if that's okay with you ? – user7420580 Jan 15 '17 at 08:48
  • @user7420580 we can communicate together in stackoverflow to help everyone that have a common question. you can update question to make an obvious one. – x01saa Jan 15 '17 at 08:58
  • @user7420580 it's not relevant to your code structure. if you have an output before `header`, it does not work. reg.html or any other files. – x01saa Jan 15 '17 at 09:02
  • I updated the code. there's no echo, print, printer above the error line. and this problem didn't get fixed ! – user7420580 Jan 15 '17 at 09:09
  • `if($_POST['Email'] !== $_POST['Email'])` ? and now add `ini_set('display_errors', 0);` to disable printing warning and errors before sending header. – x01saa Jan 15 '17 at 09:43
  • if($_POST['Email'] !== $_POST['Email']), I used this code for password then I removed password and I add email so I can have time to find a solution for the problem. Also I turned it off but the problems still doesn't want to get fixed. I don't have output so I don't really know what is the problem. I start thinking to just program a new file. but I really want to know what is the problem of this code. – user7420580 Jan 15 '17 at 09:53