SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP "Re-direct"

  1. #1
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP "Re-direct"

    Hello,

    Does anyone know how do redirect a user with PHP. For example, with ASP I might say:

    If username = "Makeda" {
    Response.Redirect("../makeda.asp")
    }

    I am looking for the PHP equivalent of the ASP "Response.Redirect". My temporary hack so far has been to dynamically write a javascript statement (window.location="../makeda.asp") but I really don't like doing this.

    Thanks,
    Makeda

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easiest way is by setting a header like so:

    header("Location: http://url/to/redirect/to");

    (note the url can be relative or absolute).

    However, here is the caveat. You cannot have sent any output whatsoever before sending a header or you will get an error "Warning: Cannot send header. Headers already sent on line X" (or something similar). This includes having no whitespace before your opening <?php tag. You can get around this by turining output buffering on. I have never done this but you can do so globally in php.ini or set buffering for the current script by calling the appropriate function (dunno exactly what that is ) at the beginning of the script. I think there is an article about output buffering at zend.com ?!?

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you for the response. Let me elaborate a little bit more about what I am doing. Essentially I am using this for a login box. The user enters a username and password. This queries MySQL to return the password and URL associated with that username.

    If the username and password don't match, it writes an error message inside of the login box. If the username matches the password, I want it to execute the "Re-Direct" action and have it redirect to the URL returned from my database.

    From what you have told me about the "Header" command, I am unsure how I can make this work if it needs to be the first line in my code. Could you elaborate?

    Thank You

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry if I was not clear. The statement:

    header("Location: url");

    does not have to be the first line in your code. It has to be the first bit of output produced by the script according to the logic of the script. For example this is fine:
    PHP Code:
    no whitespace befor the opening tag
    because it will be sent to the browser!
    <?php

    $secretword 
    "foo";

    // put any number of statements here as long as they don't send 
    // output to the browser. (ie, no echo or print statements)

    if ( $HTTP_POST_VARS["password"] != $secretword) {
       echo 
    "Your're an idiot!";
    } else {
       
    header("Location: members_index.php");
    }
    ?>
    Last edited by freakysid; Jun 11, 2001 at 05:59.


Bookmarks

Posting Permissions

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