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

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>';

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'];

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.

php redirect_to() function undefined

I am working on a database and I need to redirect the user to another page after the login process.
I am using the redirect_to() function. After the code executes it gives me the error
Fatal error: Call to undefined function redirect_to()
Here is what I tried
if($result){
$num_rows = mysqli_num_rows($result);
if($num_rows == 1){
$found_user=mysqli_fetch_array($result);
redirect_to("main.php");
}
else{
redirect_to("index.php");
}
}
You have to define the redirect_to function before calling it. Try this code
<?php
if($result){
$num_rows = mysqli_num_rows($result);
if($num_rows == 1){
$found_user=mysqli_fetch_array($result);
redirect_to("main.php");
}
else{
redirect_to("index.php");
}
}
function redirect_to($location){
header('Location:'.$location);
}
?>
Try to use header :
<?php
/* Redirection vers une page différente du même dossier */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
redirect_to() isn't a function. You want to use header("Location: main.php");
As this is adding header information, it needs to be loaded before anything is passed to the page (I believe), so don't attempt to print anything out to the page before header() is called.
If you want to do something like that, I suggest using javascript.
redirect_to doesn't exist, or if it does, it's not been included/required correctly.
Add this;
if( function_exists('redirect_to') == FALSE ) { //Future proofing...
function redirect_to($where) {
header('Location: '. $where);
exit;
}
}
If you already have output, I would advise you looking at using output buffering.
You can easily redirect using following header("Location: ....) syntax:
header("Location: main.php");
exit;
You should equip your project with this function first as others have pointed. Alternatively refactor your code to use the builtin php function -> http://php.net/manual/en/function.http-redirect.php

PHP: Redirecting upon a true statement

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.";
}

header("Location: $value"); wont redirect... Why?

After login I want to send users back to $value,
$value is generated with my code, printed and looks ok.
It's a complete URL: http://example.com/page.php?id=6,
but it ignores the header("Location: ".$value); statement:
if($iniciando->iniciar()) {
if (isset($_SESSION['redirect'])) {
$he ="http://funcook.com" . $_SESSION['redirect'];
mostrar_notificacion($he);
header("Status: 301");
header("Location: " . $he, true, 301);
} else {
imprimir_sesion_iniciada();
}
} else {
imprimir_formulario_sesion();
}
header("Location x"); has to be called before any other output is sent to the browser. This includes any spaces outside the <?php and ?> markers.
Also, make sure you also don't have any print/echo statements for debug purposes.
I suspect you're using Chrome?
You should use a better redirect code for all browsers to follow it:
header("Status: 301");
header("Location: ".$URL, true, 301);

Categories