PHP: Redirecting upon a true statement - php

Probably something simple I'm missing, but what I'm trying to do is finish up a registration form and after it sends a confirmation email, redirect it to another page. What I have so far
if($sentmail){
<redirection code>
}
else {
echo "Problem sending information, please resubmit.";
}

You want header(), and possibly an exit to stop processing.
if ($sentmail) {
header('Location: http://example.com/after_true_statement_redirected');
// exit; ???
} else {
echo "Problem sending information, please resubmit.";
}

You do this by using the header(); function.
if($sentmail){
header("Location: http://mypage.com/redirect.php");
die();
}
else {
echo "Problem sending information, please resubmit.";
}
Use die(); to stop script execution. Though the page redirects, the script still executes.

The most easy redirect call is http_redirect in PHP. Does everything you need to do for a redirect.
if ($sentmail)
{
http_redirect('new location');
}
else
{
echo "Problem sending information, please resubmit.";
}

Related

How to stop phpmailer multiple resend on refresh? (and show error message)

When i refresh the page the phpmailer always resends the email.
What i did?
Used the header("Location: home.php");
But how can i do the Location to home.php and show my error message
$error = "Thank you for message!";
if($mail->send()){
header("Location: home.php");
$error = "Thank you for message!";
} else {
$error .= "Error {$mail->ErrorInfo}";
}
the problem is that when i do the header it does not show me the error message...
<div class="text-center impact">
<?php echo $error; ?>
</div>
You can pass a GET-parameter so you can check it when the page reloads. Try this code example:
if($mail->send()){
header("Location: home.php?success");
} else {
$error .= "Error {$mail->ErrorInfo}";
}
And on your page:
<div class="text-center impact">
<?php echo isset($_GET['success']) ? "Thank you for message!" : $error; ?>
</div>
You will not see it, because your browser do redirect immediately, before you can see it.
Super simple solution will be to redirect to:
header("Location: send-confirmation.php");
with the information that message has been sent.
Of course you can do more advanced solution and pass apropiate parameter to home page or to use cookies/session on your phpmailer page to avoid duplicate sending.
You are not passing the $error variable between your pages, so when you echo it, it's not defined and you'll get no output. You need to either pass it through a URL query parameter:
header("Location: home.php?error=" . rawurlencode($error));
and then retrieve it on that page:
echo $_GET['error'];
or alternatively pass it via a session variable (probably the better choice):
$_SESSION['errors'] = $error;
header("Location: home.php");
and then:
echo $_SESSION['error'];

How to integrate 2checkout with PHP header("Location:")?

I am trying to integrate 2checkout.com API. When I skip header function then its working fine. Presently it displaying same php code as output
if ($charge['response']['responseCode'] == 'APPROVED') {
//echo "Thanks for your Order!";
header("Location: /servlet/Payment?option=1&msg=0");
exit(0);
}else{
$replyError = $charge['response']['responseCode'];
header("Location: /servlet/Payment?option=2&msg=".$replyError);
exit(0);
//echo '<pre>';print_r($charge['response']['responseCode']);echo'</pre>';
}
} catch (Twocheckout_Error $e) {echo $e->getMessage();}
You cannot emit a header after the page's html starts being emitted.
Instead of header() method use javascript method for redirection like below:
echo '<script>window.location="http://stackoverflow.com";</script>';

Write text with echo() after reloading page with header()

I have page called account_settings.php and it's consist of change password, change profile pic, change user details (name, bio etc.). My question is how to write message with echo() after redirecting page with header().
Something like this:
if (true)
{
Do_Some_MySQL();
header("Location: account_settings.php");
echo "Success!";
}
else
{
echo "Error!";
}
Thank you for all replies. ;-)
You can't actually do something after sending a Location header - it is impossible.
Instead, you could use $_SESSION array value to perform your task. Like:
if (true)
{
Do_Some_MySQL();
$_SESSION['message'] = 'Error!';
header("Location: account_settings.php");
}
else
{
echo "Error!";
}
And then on your account_setting.php:
<?php echo $_SESSION['message'] ?>
This would be nice if the account_settings.php is not the same page as you currently are. Otherwise, you could use the following code:
if (true)
{
Do_Some_MySQL();
$error = 'Success!';
header("Location: account_settings.php");
}
else
{
$error = "Error!";
}
And on the same page:
<?php if($error) echo $error; ?>
Also don't forget to include session_start() on both pages if you didn't it yet.
I would use a SESSION variable:
on redirect-page:
<?php
#session_start();
if(true){
$_SESSION['success'] = 1;
header("Location: account-settings.php");
}
?>
and on account-settings.php:
<?php
#session_start();
if(isset($_SESSION['success'])){
echo "Success!";
unset($_SESSION['success']);
}
You cannot echo anything after you just redirected. The browser is already processing the request to redirect to another page, so it doesn't bother about displaying the message anymore. What you seem to be looking for is something called flash message. You can set a temporary message in the session and have it display on the new page. For example, in your account_settings.php page:
// Make sure you have an actual session
if (!session_id()) {
session_start();
}
if (true) {
Do_Some_MySQL();
$_SESSION['flashMessage'] = 'Success!';
header('Location: account_settings.php');
}
Then in your template file for account_settings, check if there is any flash message and display it accordingly (and unset it to avoid a loop):
if (isset($_SESSION['flashMessage'])) {
echo $_SESSION['flashMessage'];
unset($_SESSION['flashMessage']);
}
These people are correct...you can't send headers after a redirect. Although I think this would be a beneficial alternative. To send a GET request in your header and process it on the receiving page. They are suggesting to use $_SESSION vars, but you can use GET vars. Ex:
if (true)
{
//Do_Some_MySQL();
header("Location: account_settings.php?message=success");
//above has GET var message = Success
}
else
{
header("Location: account_settings.php?message=error");
}
On your account_settings.php page have this code:
if (isset($_GET['message'])) {
$message = $_GET['message'];
if ($message == "success") {
echo "Success";
} else {
echo "Error";
}
}
This removes the need of CONSTANT SESSION vars. and gives you plenty of flexibility.
header("Location: account_settings.php?message=No%20results%20found");
//%20 are URL spaces. I don't know if these are necessary.
If you need you can add more then one.
header("Location: account_settings.php?message=error&reason=No%20Results&timestamp=" . Date());
then account_settings.php can be:
if (isset($_GET['message'])) {
$message = $_GET['message'];
$reason = $_GET['reason'];
$time = $_GET['timestamp'];
if ($message == "success") {
echo "Success";
} else {
echo "Error: <br/>";
echo "Reason: $reason";
}
}
But remember GET exposes your messages in the browsers URL. So DON'T send sensitive information unless you secure it. Hope this helps.

Redirect fails in PHP

Here is a piece of code in PHP written in a separate file called core.php and this is included in register.php
if(($retuserkey = $this->dbcontroller->dbregister($email, $contact)) > 0){
//if user was successfuly registered send an email where he can activate his account!
$this->mailer->sendForReg($email,$hash,$flname);
echo "<script>alert('An activation link is sent to your email id. Please check you spam folder too. Please follow the link to complete the registration.'); window.location = './registersuccess.php';</script>";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.websiteaddress.com/registersuccess.php">';
$url = "http://www.websiteaddress.com/registersuccess.php";
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url);
exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
}
mail("emailaddress#gmail.com","Registration $retuserkey",$email,"Subject");
}
Emails are sent both before and after the redirect. Both the emails are received but the redirect fails.
What am I doing wrong?
header() must be called before any actual output is sent and you have echo before it.
You can't do:
header('Location: '.$url);
exit;
after something has already been echo'd out: http://php.net/manual/en/function.header.php.
If you move your echo statements so they only print when the redirect isn't happening, then you should be OK.
header('Location: http://localhost/yourFileName.php');
exit;
header() function is used /* Redirect to a different page in the current directory that was requested */
header() function sends a raw HTTP header to a client
N.B: use right url

Echo to a specific URL instead of text message not working

I can't get to send visitors to the Thank you page using ECHO method.
Please help!
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
if (($name=="")||($email==""))
{
echo "All fields are required, please fill THE FORM again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Subscription Request from Website";
mail("test#test.com", $subject, $message, $from);
echo "Location: thankyou-subscription.php";
}
}
?>
Please use the header function to redirect users.
Example:
header("Location: thankyou-subscription.php");
You should also exit after echo "All fields are ...".
echo does not redirect unless it outputs some JavaScript code or HTML Meta Refresh. If you have to redirect via only PHP, use header and also make sure nothing is sent to browser before that header, not even a blank line
echo "Location: thankyou-subscription.php";
should be
header("Location: thankyou-subscription.php");
Edit
Since you mentioned you cannot avoid echo before redirect, you can use JavaScript because PHP redirect won't work after output. You can do
echo "<script>location.href='thankyou-subscription.php';</script>";
Don't echo, instead do this:
header("Location: thankyou-subscription.php)";

Categories