6

I have a login script that does this:

$_SESSION['username']=$username;

$_SESSION['password']=$password;

If the user logged in succesfully. And so I edited the signup page to do this:

<?php

function redirect() {
    header(' URL= index.php');
}

?>
<?php session_start(); ?>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
 <link href='icon.jpg' rel='icon' type='image/jpg'/>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <meta http-equiv="Content-language" content="en" />
 <LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
 <Title>Sign up | JMToday</title>
</head>
<body>
<?php
  if(isset($_SESSION['username'])){
   redirect();
  }
?> 

But it doesn't redirect the user when I logged in with my account that I created. Why is that?

moinudin
  • 134,091
  • 45
  • 190
  • 216
Samir Ghobril
  • 4,953
  • 4
  • 18
  • 15
  • the idea here is that you accept the answer that solves your question. I've noticed that you haven't accepted any of your questions here. Please do that, as you'll more likely get good answers if you award the people who help you. – Jan Hančič Dec 24 '10 at 10:40
  • Did you try to enable warnings? You should be getting _Warning: Cannot modify header information - headers already sent..._ – tishma Dec 24 '10 at 12:25
  • 2
    How many times have you posted this nearly identical code sample over the past few days? Please follow some of the advice you've be given by people and stop re-posting this until you've at least a clue about what you're doing. – John Parker Dec 24 '10 at 12:51
  • Dont store the password in a session... use a token something that doesn't give away all of a user credentials. – SaggingRufus Jan 31 '14 at 15:08

6 Answers6

12
header(' URL= index.php');

should be

header ( 'Location: index.php' );

Also you might want to put a die() statement after the call to header() so that you stop the execution of your script completely.

And you should probably move the call to redirect() above any other output since HTTP headers must be the first thing in the response. It's possible that this is also the cause of your problem.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • can you replace probably with obligatory? he has too, otherwise it won't work at all – Hannes Dec 24 '10 at 10:11
  • @Hannes he might have `output buffering` turned on hence "probably" :) – Jan Hančič Dec 24 '10 at 10:12
  • defining a function in a `ob` ? That would be even worse :D – Hannes Dec 24 '10 at 10:15
  • @Hannes output buffering can be turned on in php.ini and works "automagically". – Jan Hančič Dec 24 '10 at 10:18
  • *shrugs* still bad style thou – Hannes Dec 24 '10 at 10:20
  • @Hannes I agree, but this may or may not be the case here, so I have to take it into account :) – Jan Hančič Dec 24 '10 at 10:23
  • I however don't put the `die()` or `exit()` in my code. I ensure that all my code is properly inside the `if()` or `else` conditions and there is nothing which can execute after the redirection happens. I keep my main controller code mostly consisting of function calls one after the other so that there is not too much of lines of code and the scope of the `if-else` is easily visible and there is no chance of something executing by mistake and hence no need of putting an `exit` or `die` to ensure that. wanted your opinions people. – Sandeepan Nath Dec 24 '10 at 10:24
11

Change the redirect() function to:

header('Location: index.php');

And move the call to redirect above all the html output:

<?php session_start();
if(isset($_SESSION['username'])) {
    redirect();
} ?>

From the header() docs:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

This is what it should look like in the end, taking @Jan's advice to add a call to die():

<?php
function redirect($DoDie = true) {
    header('Location: index.php');
    if ($DoDie)
        die();
}
php session_start();
if(isset($_SESSION['username'])) {
    redirect();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
 <link href='icon.jpg' rel='icon' type='image/jpg'/>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <meta http-equiv="Content-language" content="en" />
 <LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
 <Title>Sign up | JMToday</title>
</head>
<body>
?> 
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • I'd move `die()` to the `redirect()` function, so that you don't have to remember to type it every time you do a redirect ... – Jan Hančič Dec 24 '10 at 10:15
  • @Jan I considered that, but do you want to restrict to being the last thing you ever call? I'm taking into account that you might later use the `redirect()` function elsewhere. – moinudin Dec 24 '10 at 10:16
  • 2
    Maybe you could pass a `$DoDie` parameter to the `redirect()` function. Anways, I've never came across a situation where I did a redirect and wanted to execute some code after that. But that's just me :) – Jan Hančič Dec 24 '10 at 10:18
5
function redirect() {
    header('location:index.php');
}
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

It's header('Location: index.php');

Core Xii
  • 6,270
  • 4
  • 31
  • 42
0

if you want to redirect immediately, then

function redirect(){
    header("Location: home.php");
}

if you want to redirect with some delay, then

function redirect(){
    header("Refresh: 0;url=default.php");
}

increase the 0 in "Refresh:0" to introduce greater delay.

you might use this to redirect after showing some notification/message to the user.

Note if you are having some trouble in redirecting, then put "exit()" at the end of function

diwakar
  • 21
  • 4
0
<?php
  session_start();

  if((isset($_SESSION["username"]))) 
  {
   header("Location: home.php");
  }
?>
Adumuah Dowuona
  • 121
  • 1
  • 11