Redirecting in PHP without using a header method

This contact.php form worked to handle sending, then redirecting to a new page, and then suddenly just stopped working. I tried to add error handling as well as move the header at the top in front of all the others, but none of them work. The form is still submitting data, as expected, it's just a redirect that doesn't work. Any ideas would be appreciated.

<?php include 'config.php'; $post = (!empty($_POST)) ? true : false; if($post) { $email = trim($_POST['email']); $subject = "Downloaded Course Units"; $error = ''; if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$email."\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; header('location: http://www.google.com.au/'); exit(); } } } ?> 
+14
php header location
source share
5 answers

Use javascript.

Instead

 header('location: http://www.google.com.au/'); 

Using

 ?> <script type="text/javascript"> window.location.href = 'http://www.google.com.au/'; </script> <?php 

It will be redirected even if something is displayed in your browser.

But one precaution is to be taken: Javascript redirection will redirect your page, even if something is printed on the page.

Make sure it doesn't skip any logic written in PHP.

+25
source share

Replace the header('location: http://www.google.com.au/'); line header('location: http://www.google.com.au/'); to the code below to redirect to php without using the header function.

 $URL="http://yourwebsite.com/"; echo "<script type='text/javascript'>document.location.href='{$URL}';</script>"; echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">'; 

If you are wondering why I used the meta tag and JavaScript to redirect , then the answer is very simple.

If JavaScript is disabled in the browser, the meta tag redirects the page.

+17
source share

the header does not work after include, echo. try again without turning on, echo. OR use the function header instead

 echo '<meta http-equiv="refresh" content="0; URL=http://www.google.com.au/">'; 
+4
source share

If you want to redirect to another page after the HTML code, use the javascript location.href method.

Refer to this code example:

 <html> <head> <title> Using the href property of the Location object</title> </head> <body> <script language="JavaScript"> <!-- function show(){ document.location.href ="http://www.java2s.com"; } --> </script> <form name="form1"> <br> <input type="button" name="sethref" value="Set href" onClick='show()'> <br> </form> </body> </html> 
+3
source share

I solved this with:

 function GoToNow ($url){ echo '<script language="javascript">window.location.href ="'.$url.'"</script>'; } 

Use: GoToNow (' http://example.com/url-&error=value ');

+3
source share

All Articles