2

I have a variable with a url,

$order_paypage_url = "example.com"

I want to redirect from this to this url from website, I have tried this :

header('Location: '.$order_paypage_url);
exit;

But when I run this session refreshes and the redirect does not occur.

More relevant information : I am working on a PHP based e-commerce site and this is in relevance to redirecting after clicking on Place Order. I have the target destination as url, I just want to know what is triggering this session refresh which is keeping me from reaching this URL. And how can i circumvent this problem.

jojostev97
  • 104
  • 2
  • 8
  • 4
    Please add more details - is your url an external site? do you have a specific session handling? – Roman Mar 21 '21 at 08:32
  • 1
    I'm pretty certain that user agents would interpret that specific header as a relative page, not a domain. I think you can make it protocol-less but you'd have to test that. This might not be related to your actual problem, but it is something that you should look into regardless – Chris Haas Mar 22 '21 at 14:09
  • you can not send header once you already sent any content. The session belongs to the source website so not available at the target one. on what page are you trying to retrieve the "lost" session data ? – Tuckbros Mar 26 '21 at 10:26
  • 2
    what do you mean by "session refreshes"? – Salman A Mar 26 '21 at 12:29

3 Answers3

4

Perhaps adding HTTP status code 301 or 302 from 3xx redirection could help:

<?php
$order_paypage_url = 'https://www.example.com/';
header("Location: $order_paypage_url", TRUE, 301);
exit;

Try also to use an absolute address that starts with a sheme https:// for a secure connection or http:// for an insecure one.

An alternative approach may be to use HTML meta redirect as described at this website and this question or use JavaScript redirect

if multiple redirections used the importance is as follow:

  1. HTTP redirects always execute first — they exist when there is not even a transmitted page.
  2. HTML redirects () execute if there weren't any HTTP redirects.
  3. JavaScript redirects execute last, and only if JavaScript is enabled.

You may also configure redirections at your web server in a such way that if a user clicks a button (link) then his browser sends request to the web server which redirects without even involving the PHP for that.

For checking if you generate a proper redirect response

enter image description here

to the client you may use Firebug for the Firefox browser and its Network tab

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • When you send a `Location: ` header, PHP _also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set._ Something else is wrong. – Salman A Mar 26 '21 at 12:33
1

You can use javascript also

<?php
$order_paypage_url = "example.com"
echo "<script>window.location.href = {$order_paypage_url};</script>";
?>
L3xpert
  • 1,109
  • 1
  • 10
  • 19
0

You can use HTML Redirect Meta tag And Javascript redirect some time header location function not works some time But these solutions 100% works .

<?php
$order_paypage_url = "example.com";

echo '<meta http-equiv="refresh" content="0; url='".$order_paypage_url."'" />';

?>

OR
<?php
$order_paypage_url = "example.com";
?>
<meta http-equiv="refresh" content="0; url=<?php echo $order_paypage_url; ?>" />';

 And
<?php 
$order_paypage_url = "example.com";

?>

<script>window.location.href = "<?php echo $order_paypage_url; ?>";</script>

Enjoy ;)

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24