1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP If/then statement

Discussion in 'Web Design & Development' started by tabspace, Jan 29, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. tabspace

    tabspace Thread Starter

    Joined:
    Mar 23, 2006
    Messages:
    54
    Hello,

    I am trying to modify a login script to redirect based on username.

    currently the script will only redirect to a folder that is related to either the username or password. i want to set it up so that the folder/file name does not have to match the username so that multiple users can login or that administrators can login

    at the bottom of the script there is a line that says

    header('Location: http://www.site.com/temp/'.$login.''); die();

    i would like something more like

    if $login = username1,username2
    redirect http://www.site.com/art/folder1

    else if $login = username3
    redirect http://www.site.com/files/folder

    else if $login = admin
    redirect http://site.com/files

    etc.
     
  2. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    PHP:
    <?php
    if ($login == username1 OR username2) {
    header('Location: http://www.site.com/art/folder1');
    exit;
    }
    elseif (
    $login == username3) {
    header('Location: http://www.site.com/files/folder');
    exit;
    }
    elseif (
    $login == admin) {
    header('Location: http://site.com/files');
    exit;
    }
    ?>
    You mean something like that?
     
  3. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    That would take you to folder1 every time
    PHP:
    <?php
    if ($login == 'username1' OR $login == 'username2') {
    header('Location: http://www.site.com/art/folder1');
    exit;
    }
    elseif (
    $login == 'username3') {
    header('Location: http://www.site.com/files/folder');
    exit;
    }
    elseif(
    $login == 'admin') {
    header('Location: http://site.com/files');
    exit;
    }
    ?>
    If there are a lot of usernames, you might want to look at using a switch statement or arrays for this though.
     
  4. tabspace

    tabspace Thread Starter

    Joined:
    Mar 23, 2006
    Messages:
    54
    thanks. not planning for too many usernames. looks like this should work.
     
  5. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012

    thanks for showing me how to do the OR, I wasn't sure about that one :)
     
  6. tabspace

    tabspace Thread Starter

    Joined:
    Mar 23, 2006
    Messages:
    54
    works. thank you much
     
  7. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    No problem, it's that "OR username2" is always going to evaluate to true.
     
  8. tabspace

    tabspace Thread Starter

    Joined:
    Mar 23, 2006
    Messages:
    54
    i found the way to do the array as well

    PHP:
    $REDIRECTS = array(
      
    'username1' => 'art/folder1',
      
    'username2' => 'art/folder1,
      '
    username3' => 'files/folder',
      '
    admin' => 'files'
    );

    Than add following at the very bottom of the script, just before the ?> line
    header('
    Locationhttp://www.site.com/'.$REDIRECTS[$login]); exit;
    thanks again for all the help
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/539241

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice