How to properly redirect to the same page in php?

I am working on something that i want to use in a project. i am to redirect the current page to itself using php and session. The idea is that:

[php]if my $_session[‘insert’]= true; then refresh the page to itself;[/php]

Here is the code:
Page1:
[php]sess_page1.php

<?php session_start(); $_SESSION["insert"] = 2; $_SESSION["test"] = "Page refreshed"; ?>

Click here to go to the next page[/php]

Page2:
[php]sess_page2.php

<?php session_start(); if (isset($_SESSION["insert"]) && ($_SESSION["insert"]==2)) { header('location:'.$_SERVER['PHP_SELF']); print $_SESSION["test"]; } else { echo "Impossible to execute."; exit(); } ?>

[/php]
But the page does not redirect to itself…The whole idea is the page should refresh one time.

What i did wrong here? Can somebody help?

I think the problem relies in your ‘location’ header in sess_page2.Which is not redirecting the page properly.

try this
header(“http://” . "Location: " . $_SERVER[‘HTTP_HOST’] .$_SERVER[‘PHP_SELF’]);

this will redirect to the same page you are requesting.

header("Location: " . “http://” . $_SERVER[‘HTTP_HOST’] . $location

where $location is the path after the domain, starting with /.

and another way for redirecting the page is,

header('Location: '.$_SERVER[‘PHP_SELF’]);
die;

Don’t forget to die or exit after your header();

If the query string is also necessary, use
header(‘Location:’.$_SERVER[‘PHP_SELF’].’?’.$_SERVER[‘QUERY_STRING’]);
die;

Maulik, the “die;” do nothing…

KB, just hard-code the page, do not use the $_SERVER[] array…

header(“location: sess_page2.php”);

But, that code is silly, too. You load a page and immediately refresh it? It would just keep looping.
What are you trying to do with that code? Makes no sense. If you want to refresh a page upon loading
that same page, why?

Sponsor our Newsletter | Privacy Policy | Terms of Service