Whats wrong with this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Whats wrong with this?

Post by ianhull »

Code: Select all

if ($_SESSION['level'] == "User") {
echo '<meta http-equiv="refresh" content="0;URL=user/control_panel.php">';
} 
elseif ($_SESSION['level'] == "Manager") {
echo '<meta http-equiv="refresh" content="0;URL=manager/control_panel.php">';
}
elseif ($_SESSION['level'] == "Creator") {
echo '<meta http-equiv="refresh" content="0;URL=creator/control_panel.php">';
}
else {($_SESSION['level'] == "Owner")
echo '<meta http-equiv="refresh" content="0;URL=owner/control_panel.php">';
}
thanks
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Relative urls instead of absolute urls.

But more generally, what problems are you having with it?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Whats wrong with this?

Post by aerodromoi »

ianhull wrote:

Code: Select all

if ($_SESSION['level'] == "User") {
echo '<meta http-equiv="refresh" content="0;URL=user/control_panel.php">';
} 
elseif ($_SESSION['level'] == "Manager") {
echo '<meta http-equiv="refresh" content="0;URL=manager/control_panel.php">';
}
elseif ($_SESSION['level'] == "Creator") {
echo '<meta http-equiv="refresh" content="0;URL=creator/control_panel.php">';
}
else {($_SESSION['level'] == "Owner")
echo '<meta http-equiv="refresh" content="0;URL=owner/control_panel.php">';
}
thanks
You don't need a condition for else.

aerodromoi
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Whats wrong with this?

Post by infolock »

ianhull wrote:

Code: Select all

if ($_SESSION['level'] == "User") {
echo '<meta http-equiv="refresh" content="0;URL=user/control_panel.php">';
} 
elseif ($_SESSION['level'] == "Manager") {
echo '<meta http-equiv="refresh" content="0;URL=manager/control_panel.php">';
}
elseif ($_SESSION['level'] == "Creator") {
echo '<meta http-equiv="refresh" content="0;URL=creator/control_panel.php">';
}
else {($_SESSION['level'] == "Owner")
echo '<meta http-equiv="refresh" content="0;URL=owner/control_panel.php">';
}
thanks

As It's quite obvious what you mean by "Whats wrong with this?", mainly you are using multiple if/ifelse commands, and not resuing your code correctly..

You could actually use a switch statement and reduce the amount of data you are defining by just defining a static variable to hold a alternating value such as your URL.

It just makes it more clean and robust, like so :

Code: Select all

$path  = NULL;

switch(strtolower($_SESSION['level'])) {
  case 'user':
    $path = 'user/control_panel.php';
  break;

  case 'manager':
    $path = 'manager/control_panel.php';
  break;

  case 'creator':
    $path = 'creator/control_panel.php';
  break;

  default:
    // Set $path to be whatever it should be for any other condition not met above.   In this case, I assume you are setting anything that doesn't meet the first requirements to be the owner.

    $path = 'owner/control_panel.php';
  break;
}

$meta_information = '<meta http-equiv="refresh" content="0;URL=' . $path . '">';

hope this helps.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Whats wrong with this?

Post by RobertGonzalez »

ianhull wrote:

Code: Select all

if ($_SESSION['level'] == "User") {
echo '<meta http-equiv="refresh" content="0;URL=user/control_panel.php">';
} 
elseif ($_SESSION['level'] == "Manager") {
echo '<meta http-equiv="refresh" content="0;URL=manager/control_panel.php">';
}
elseif ($_SESSION['level'] == "Creator") {
echo '<meta http-equiv="refresh" content="0;URL=creator/control_panel.php">';
}
else {($_SESSION['level'] == "Owner")
echo '<meta http-equiv="refresh" content="0;URL=owner/control_panel.php">';
}
thanks
Can you do this...

Code: Select all

<?php
echo '<meta http-equiv="refresh" content="0;URL=http://www.mysite.com/' . strtolower($_SESSION['level']) . '/control_panel.php">';
?>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Whats wrong with this?

Post by infolock »

Everah wrote:
ianhull wrote:

Code: Select all

if ($_SESSION['level'] == "User") {
echo '<meta http-equiv="refresh" content="0;URL=user/control_panel.php">';
} 
elseif ($_SESSION['level'] == "Manager") {
echo '<meta http-equiv="refresh" content="0;URL=manager/control_panel.php">';
}
elseif ($_SESSION['level'] == "Creator") {
echo '<meta http-equiv="refresh" content="0;URL=creator/control_panel.php">';
}
else {($_SESSION['level'] == "Owner")
echo '<meta http-equiv="refresh" content="0;URL=owner/control_panel.php">';
}
thanks
Can you do this...

Code: Select all

<?php
echo '<meta http-equiv="refresh" content="0;URL=http://www.mysite.com/' . strtolower($_SESSION['level']) . '/control_panel.php">';
?>
you could, but if the directory URL ever changes, or becomes different per instance, the above will result in error.


edit: The key to any coding practice is to train yourself to code expecting changes to be made later down the road, which would result in fewer lines to edit and more control of what/how data s used.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That could be said about the OP's code as well, seeing as all my code does is compact his list of if/elseif's into one simple string. I was just responding to that (somewhat vague) question.

Looking at what it seems to be doing, I would guess that, dependent on a given users level, the user would be redirected to a unique control panel inside a folder that is named for their level. Would I do this? Not on your life. I would setup one control panel and figure out what to offer the user depending on their level inside that one control panel.

But given the OP's original question, 'What is wrong with this?', well, not a whole lot other than it seems to be really inefficient. I would just make it a little more simple, which is what I did in my code example.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$allowed = array('user', 'manager', 'creator', 'owner');
$level = strtolower($_SESSION['level']);
if (in_array($level, $allowed)) {
    echo '<meta http-equiv="refresh" content="0;URL=http://www.mysite.com/' . $level . '/control_panel.php">';
}
(#10850)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Everah wrote:That could be said about the OP's code as well, seeing as all my code does is compact his list of if/elseif's into one simple string. I was just responding to that (somewhat vague) question.

Looking at what it seems to be doing, I would guess that, dependent on a given users level, the user would be redirected to a unique control panel inside a folder that is named for their level. Would I do this? Not on your life. I would setup one control panel and figure out what to offer the user depending on their level inside that one control panel.

But given the OP's original question, 'What is wrong with this?', well, not a whole lot other than it seems to be really inefficient. I would just make it a little more simple, which is what I did in my code example.
100% agree. However, a lot of beginner level coders begin writing code such as this as an indirect means of achieving a solution for something later.

To reitterate what you said, a simple solution is often the best in almost every instance of development. Write it, make it work, and then rewrite it.

But to reitterate what I said, it's also good to write towards a more dynamic approach for obsticals later on down the road.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Agreed. Foresight in planning is a rather keen piece of advice. Of course, most new developers want to know "how to make this do what I want it to", not "how to make this do what I want it to in the best way possible" (yes, I was one of those types of newbies).
Post Reply