PHP If/then statement

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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.
 
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?
 
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.
 

tabspace

Thread Starter
Joined
Mar 23, 2006
Messages
54
thanks. not planning for too many usernames. looks like this should work.
 
Joined
Apr 30, 2005
Messages
3,012
brendandonhu said:
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.

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

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('Location: http://www.site.com/'.$REDIRECTS[$login]); exit;
thanks again for all the help
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top