How to redirect to previouspage after sending mail in php

To redirect to the previous page after sending an email in PHP, you can use the header() function to send a redirect response to the browser. Here's an example:

<?php
// Your email sending code here
mail('[email protected]', 'Subject', 'Message');

// Redirect to previous page
header('Location: '. $_SERVER['HTTP_REFERER']);
exit;
?>

In this example, $_SERVER['HTTP_REFERER'] contains the URL of the previous page that referred the user to the current page. The header() function sends a redirect response to the browser, and the exit statement ensures that the script terminates immediately after sending the redirect.

Note that $_SERVER['HTTP_REFERER'] may not always contain the correct URL of the previous page, as some browsers or proxies may not send this information. In such cases, you can use a session variable or a hidden form field to store the previous page's URL and then redirect to it.

Alternatively, you can use the header() function with a specific URL to redirect to:

header('Location: previous-page.php');
exit;

Make sure to replace previous-page.php with the actual URL of the page you want to redirect to.

Also, keep in mind that the header() function must be called before any output is sent to the browser, so make sure to put the redirect code before any HTML or output is generated.