Prateek Dubey
Prateek Dubey

Reputation: 11

Header function not working PHP

I redirecting a page after successful registration the code is working everythig work properly but the header function not working & page not redirect. the page not get redirect .my code is given below

  <?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING);

//include("lib/local_config.php");
include("lib/config.php");


$activation=md5(uniqid(rand(), true));

if(isset($_POST["tb_submit"]))
{
    $fname=$_POST["tb_fname"];
    $email=$_POST["tb_email"];
    $pass=$_POST["tb_pass"];
    $repass=$_POST["tb_repass"];
    $contact=$_POST["tb_contact"];

    $select_email=mysql_query("select email from user_reg where email='$email'");
    $rows=mysql_fetch_row($select_email);
    $e_id=$rows[0];

    if(!$email==$e_id && $pass==$repass)
    {
        $insert="insert into user_reg ( fname ,email ,password ,contact,Activation)    values      ('$fname' , '$email' , '$pass' ,'$contact' ,'$activation')";
        $insert_query=mysql_query($insert);

        if(mysql_affected_rows($connect) == 1)
        //if(mysql_affected_rows() == 1)
        {
            $to = $_POST['tb_email'];
            $subject = "Fundu App Creator - Registration confirmation mail ";
            $message = <<<DEMO
                      <html>
                      <head>

                      <link href='http://www.fundumobi.com/app/templatemo_style.css' type='text/css'  
                       rel='stylesheet' />
                     <link href='http://www.fundumobi.com/app/styles/frm.css' rel='stylesheet' 
                       type='text/css' />
                     <style type='text/css'>

                     .style1 {
                    font-size: 14px;
                    font-style: italic;
                    font-weight: bold;

                      }
                      .style2 {color: #FFFFFF}

                       </style>
                      </head>
                     <body>
                     <table width='50%' border='0' align='center'>
                     <tr>
                      <th height='100' bgcolor='#006784'>
                      <div id='site_logo'></div>    </th>
                     </tr>
                    <tr>
                    <td height='247'><div >
                      <div align='center' class='style1'>
                        <p>Your Registration is successful !!!! </p>
                        <p>Now Please click the following Button to complete the registration process:</p>
                        <p>&nbsp;</p><br/>
                        <div ><a href='http://www.fundumobi.com/app/activation.php?act=$activation'>      
                       <img src='images/button66535179.png'/></a></div>
                      </div></td>
                     </tr>
                     <tr>
                       <td height='64' bgcolor='#006784'><div 
                      align='center'>www.funduappcreator.com</div>    </td>
                     </tr>
                    </table>
                    </body>
                    </html>
                    DEMO;

            $from = "[email protected]";

            mail($to, $subject, $message, 'From:'.$from);
            header("location:mesg.php");
        }
        else
        { // If it did not run OK.
            header("location:index.php?er=2");
        }
    }
}

?>

Upvotes: 1

Views: 7822

Answers (6)

Rech K
Rech K

Reputation: 7

Use this. It worked for me. Place this function at the top of your code:

function move_to(){
    header("Location:page_example.php");
}

and call that function anywhere:

move_to()

Upvotes: 1

Shinto Joseph
Shinto Joseph

Reputation: 1171

try this this will surely work

<script type="text/javascript">
<!--
   window.location="http://www.newlocation.com";
//-->
</script>

Upvotes: -1

Shahrokhian
Shahrokhian

Reputation: 1114

use headers_sent function to detect problem. http://php.net/manual/en/function.headers-sent.php

Checks if or where headers have been sent.

You can't add any more header lines using the header() function once the header block has already been sent. Using this function you can at least prevent getting HTTP header related error messages. Another option is to use Output Buffering.

// An example using the optional file and line parameters, as of PHP 4.3.0
// Note that $filename and $linenum are passed in for later use.
// Do not assign them values beforehand.
if (!headers_sent($filename, $linenum)) {
    header('Location: http://www.example.com/');
    exit;

// You would most likely trigger an error here.
} else {

    echo "Headers already sent in $filename on line $linenum\n" .
          "Cannot redirect, for now please click this <a " .
          "href=\"http://www.example.com\">link</a> instead\n";
    exit;
}

for your code :

<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING);

//include("lib/local_config.php");
include("lib/config.php");


$activation=md5(uniqid(rand(), true));

if(isset($_POST["tb_submit"]))
{
    $fname=$_POST["tb_fname"];
    $email=$_POST["tb_email"];
    $pass=$_POST["tb_pass"];
    $repass=$_POST["tb_repass"];
    $contact=$_POST["tb_contact"];

    $select_email=mysql_query("select email from user_reg where email='$email'");
    $rows=mysql_fetch_row($select_email);
    $e_id=$rows[0];

    if(!$email==$e_id && $pass==$repass)
    {
        $insert="insert into user_reg ( fname ,email ,password ,contact,Activation)    values      ('$fname' , '$email' , '$pass' ,'$contact' ,'$activation')";
        $insert_query=mysql_query($insert);

        if(mysql_affected_rows($connect) == 1)
        //if(mysql_affected_rows() == 1)
        {
            $to = $_POST['tb_email'];
            $subject = "Fundu App Creator - Registration confirmation mail ";
            $message = <<<DEMO
                      <html>
                      <head>

                      <link href='http://www.fundumobi.com/app/templatemo_style.css' type='text/css'  
                       rel='stylesheet' />
                     <link href='http://www.fundumobi.com/app/styles/frm.css' rel='stylesheet' 
                       type='text/css' />
                     <style type='text/css'>

                     .style1 {
                    font-size: 14px;
                    font-style: italic;
                    font-weight: bold;

                      }
                      .style2 {color: #FFFFFF}

                       </style>
                      </head>
                     <body>
                     <table width='50%' border='0' align='center'>
                     <tr>
                      <th height='100' bgcolor='#006784'>
                      <div id='site_logo'></div>    </th>
                     </tr>
                    <tr>
                    <td height='247'><div >
                      <div align='center' class='style1'>
                        <p>Your Registration is successful !!!! </p>
                        <p>Now Please click the following Button to complete the registration process:</p>
                        <p>&nbsp;</p><br/>
                        <div ><a href='http://www.fundumobi.com/app/activation.php?act=$activation'>      
                       <img src='images/button66535179.png'/></a></div>
                      </div></td>
                     </tr>
                     <tr>
                       <td height='64' bgcolor='#006784'><div 
                      align='center'>www.funduappcreator.com</div>    </td>
                     </tr>
                    </table>
                    </body>
                    </html>
DEMO;

            $from = "[email protected]";

            mail($to, $subject, $message, 'From:'.$from);
            if (!headers_sent($filename, $linenum)) 
            {
                header("location:mesg.php");
            }
            else
            {
                echo "Headers already sent in $filename on line $linenum\n" .
                  "Cannot redirect, for now please click this <a " .
                  "href=\"http://www.example.com\">link</a> instead\n";
                exit;
            }
        }
        else
        { // If it did not run OK.
            header("location:index.php?er=2");
        //use exit after redirection request
        exit;
        }
    }
}

?>

p.s : also take a look at "DEMO;" position in your code, it should be in the start of line,shouldn't it ?. use exit after your redirection. remove whitespace before your

Upvotes: 0

Arslaan Ejaz
Arslaan Ejaz

Reputation: 1001

try, using output buffer

<?php ob_start() ?>

at the top of this code

Upvotes: 1

Nick
Nick

Reputation: 8309

You should have an exit; after your redirect code. See this comment.

Upvotes: 1

MrCode
MrCode

Reputation: 64526

You can't send headers after outputting content. Here you are outputting whitespcace before the PHP tag:

  <?php
^^ remove this whitespace

Check your error log and you'll find:

Warning: Cannot modify header information - headers already sent

Upvotes: 3

Related Questions