header ("location: /login.php");
Results 1 to 15 of 15

Thread: header ("location: /login.php");

  1. #1
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    header ("location: /login.php");

    Hi

    Im trying to redirect from a loginpage.
    i have tried with header ("location: /login.php");

    but I get an errormessage saying

    Warning: Cannot add header information - headers already sent by (output started at C:/XITAMI/webpages/checklogin.php:12) in C:/XITAMI/webpages/checklogin.php on line 24

    this is the only code on the page
    <?
    $db = mysql_connect("localhost", "root");
    mysql_select_db("highway",$db);
    $result = mysql_query("SELECT * from user where name='$login' and passwd='$passwd'",$db);
    if('echo mysql_num_rows($result)'==0)
    {
    Header("Location: login.php");
    }
    else
    {
    Header("Location: test.php");
    }
    ?>

  2. #2
    Member
    Join Date
    Jun 2001
    Posts
    62

    RE: header ("location: /login.php");

    Hi.
    if('echo mysql_num_rows($result)'==0)
    ^
    Echo mean output sent to the client.Evaluate without it.
    if(mysql_num_rows($result)==0) or
    if(!mysql_num_rows($result))
    The second line is good because any value other 0 is true(even -1).0 always is treated as False.
    Hope this help you

  3. #3
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    Hi
    thanks for the tip. unfortunally it didnt do the job. the error according to the browser appers on line 9 where the <? tag is.

    Im cunfused !!!

  4. #4
    Senior Member
    Join Date
    Jan 2001
    Posts
    9,761

    RE: header ("location: /login.php");

    Read The Fantastic Manual:

    http://www.php.net/manual/en/function.header.php

    Remember that the header() function must be called before any actual output is sent, either by normal HTML tags blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file
    access function, and have spaces or empty lines that will output before header() is called. The same problem exists when using a single PHP/HTML file.

  5. #5
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    Hi
    Ive allready read the manual.
    maybe Im stupid but I have taken away all unnessary code,even tried putting it all on the same line with no spaces or anything between.it wont work.Could someone please write som example code.




  6. #6
    Member
    Join Date
    Jun 2001
    Posts
    62

    RE: header ("location: /login.php");

    This is because the error is in the if() statement.Sometime php interpreter get confusing with the line error .
    Just put one of the statements that i write in the first message.Must work.

  7. #7
    Senior Member
    Join Date
    Jan 2001
    Posts
    554

    RE: header ("location: /login.php");

    The <?php has to be the VERY first line of your file. No lines of any kind, code or just blank ones can come before it, or the php engine will send a content-type: text/html header before your location: header can go out.

    line 0: <?php
    line 1: header ("location: somewhere");

    Not:

    line 0:
    line 1: <?php
    line 2: header...


    The first will work, the second will fail when php sees a blank line and sends a content header before ever getting to the <?php part.

  8. #8
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    Okej
    but doesnt that take away the hole point with the function. cause I need to check if the user and passwd is valid before I redirect him.

  9. #9
    Member
    Join Date
    Jun 2001
    Posts
    62

    RE: header ("location: /login.php");

    Ok.
    The statement must be like this:
    <?php
    //the rest of the code came here.
    if(mysql_num_rows($result)==0)
    {
    Header("Location: login.php");
    }
    else
    {
    Header("Location: test.php");
    }
    ?>
    or you can try this too:
    <?php
    if(mysql_num_rows($result)>0)
    {
    Header("Location: test.php");
    }
    else
    {
    Header("Location: login.php");
    }
    ?>

    That's it.Musr work.

  10. #10
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    <?php
    $db=mysql_connect("localhost","root");
    mysql_select_db("highway",$db);
    $result=mysql_query("SELECT * FROM user WHERE name='$login' AND passwd='$passwd'",$db);
    if(mysql_num_rows($result)==0)
    {
    Header("Location: login.php");
    }
    else
    {
    Header("Location: test.php");
    }
    ?>

    If I exclude the html tag the page cannot be found. where does it fit in.If I put it in on top or just under the <? tag I get the same error as before.

  11. #11
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    Hi again
    Thank you all for your help.I solved it using javascript insead.

    Roger

  12. #12
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header ("location: /login.php");

    Hi..

    You guys are messing way too much with somethin' unneccessary...

    Use your original php code and do like this:

    <?php

    ob_start();

    // The code pasted before...
    // with the error msg about header and stuff

    ob_end_flush();

    ?>

  13. #13
    Senior Member
    Join Date
    Jan 2001
    Posts
    9,761

    RE: header ("location: /login.php");

    Output buffering is not a solution, it's a workaround.

    Instead of writing intelligent code that doesn't print anything when it's not required, you let output-buffering sort it out for you.
    Bad practice.

  14. #14
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: header (\

    yoy can\'t use the headers when you have already outputted data (echo, escapin to html, print,...).

    --> delete all echo\'s and it\'s solved

  15. #15
    Senior Member
    Join Date
    Jan 2001
    Posts
    1,118

    RE: header ("location: /login.php");

    You absolutely do NOT want an &lt;html&gt; tag anywhere before the call to header(). And probably not anywhere in the current php file at all, since it seems to redirect to one of two places.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •