-1

Basically I just want to go to the same file I used for the header. Here is my example code:

$_SESSION['user_id'] = $login;
header("Location: index.php");
die();

That same code is in index.php. I want to just reload the page. How would I do that?

  • why die() and what is the issue with this code? – Dhaval Chheda Aug 02 '17 at 04:16
  • Is there any condition that when you want to reload/redirect or when you don't want? – lkdhruw Aug 02 '17 at 04:19
  • Ok, what I'm trying to do is header into a page I am already in. I keep getting this error: Cannot modify header information - headers already sent – Ashley Howard Aug 02 '17 at 04:21
  • @Ikdhruw when I hit "Login" I should be redirected to the index.php – Ashley Howard Aug 02 '17 at 04:21
  • did you try searching with 'Cannot modify header information - headers already sent' ? – ASR Aug 02 '17 at 04:23
  • Yes I tried, I am still stuck on my code though. @ASR – Ashley Howard Aug 02 '17 at 04:26
  • **1st :** you missed to `start the session` .so while error thrown buffer .once your code started out put to buffer while you trying to redirect another page . so while this `error will occur` . **2nd :** you need to start the `session` each page . – JYoThI Aug 02 '17 at 07:16
  • @paul... not a duplicate.. that link is another question with the same answer, that doesn't mean it's the same question... – patrick Aug 02 '17 at 09:47

6 Answers6

0

it s tested and work

it refresh once :

<?php
if(! isset( $_GET["refresh"] ) ) {
header( "Location: index.php?refresh=1" );
exit;
}

?>

0

You're possible to redirect to the same file. But you must add some condition of when will process the redirect to prevent the non stop redirect in the same page.

  $_SESSION['user_id'] = $login;

    if(CONDITION)
    {
      header("Location: index.php");
    }

    die();
d3no
  • 121
  • 1
  • 3
  • 12
0

Try this code:

 header('Location: '.$_SERVER['REQUEST_URI']);
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
0

Cannot modify header information - headers already sent means that your code has already sent something to the browser window. Once that happens, you can't send headers any more. Look at your script and identify what sent something to the browser, and use your header() call before that.

FKEinternet
  • 1,050
  • 1
  • 11
  • 20
0

I found the solution! Thank you everyone!

The answer was this:echo "<meta http-equiv='refresh' content='0'>";

0

1st : you missed to start the session .so while error thrown to browswer .once your code started out put to browser .while you trying to redirect another page . so while this error will occur .

2nd : you need to start the session each page .

start_session();
$_SESSION['user_id'] = $login;
header("Location: index.php");
exit();
JYoThI
  • 11,977
  • 1
  • 11
  • 26