Jump to content

[SOLVED] Tidy Code


cheechm

Recommended Posts

        <? if ($_GET['id'] != 5 and $_GET['id'] != 4 and $_GET['id'] !=
    'updateprofile' and $_GET['id'] != 'login' and $_GET['id'] != 'register' and $_GET['id'] !=
    'members' and $_GET['id'] != 'manage' and $_GET['id'] != 'forgot' and $_GET['id'] !=
    'confirm')
{
    echo '<p class="block" id="form_box">';
} else
{
    echo '<p class="block">';
} ?>

 

    <?php $id = $_GET['id'];
switch ($id)
{
    default:
        include ('pages/home.php');
        break;
    case "2":
        include ('pages/talltape.php');
        break;
    case "3":
        include ('pages/videos.php');
        break;
    case "4":
        include ('pages/shop.php');
        break;
    case "5":
        include ('pages/contact.php');
        break;
    case "6":
        include ('pages/abouttt.php');
        break;
    case "7":
        include ('pages/howtousett.php');
        break;
    case "8":
        include ('pages/personalisett.php');
    case "login":
        include ('login/login.php');
        break;
    case "logout":
        include ('login/logout.php');
        break;
    case "manage":
        include ('login/manage_users.php');
        break;
    case "members":
        include ('login/members.php');
        break;
    case "register":
        include ('login/register.php');
        break;
    case "updateprofile":
        include ('login/update_profile.php');
        break;
    case "adminoption":
        include ('login/admin_options.php');
        break;
    case "forgot":
        include ('login/forgot_password.php');
        break;
    case "confirm":
        include ('login/confirm.php');
        break;
}
php ?>

 

Can I tidy these codes, or is that the best way to do it?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/103740-solved-tidy-code/
Share on other sites

 

<?php
$options = array(5, 4, 'updateprofile', 'login', 'register', 'members', 'manage', 'forgot', 'confirm');
if (in_array($_GET['id'], $options)) echo '<p class="block">';
else echo '<p class="block" id="form_box">';
?>

 

<?php
$options = array('2'=>'pages/talltape.php', '3'=>'pages/videos.php', ....); // put the rest of values
$id = $_GET['id'];
if (array_key_exists($id, $options)) include($options[$id]);
else include ('pages/home.php');
?>

Link to comment
https://forums.phpfreaks.com/topic/103740-solved-tidy-code/#findComment-531150
Share on other sites

Wow, that is the long way...

 

Consider...

 

<?php
   $valid_options = "home,talltape,videos,shop,abouttt,howtousett,personalizett,login,logout,manage_users,members,register,update_profile,admin_options,forgot_password,confirm";

  $valid_array = explode(",",$valid_options);

  if (in_array($_REQUEST["page"],$valid_array)) {
     echo '<p class="block" id="form_box">\n';
     include("pages/". $_REQUEST["page"] .".php"); 
     echo '</p>\n';
  } else
      echo '<p class="block" id="form_box"> Unauthorized Access, Please Login</p>';
  } 

?>

 

Consider that your login pages are still regular pages, and therefore can be in the same directory as other pages.  If you want to make them more secure, you can move the login pages to a "https://" designated webpage with SSL encription.  

 

All you would need then is to remove the login pages from the $valid_array, and make an additional login array to check if the user is logging in.

Link to comment
https://forums.phpfreaks.com/topic/103740-solved-tidy-code/#findComment-531156
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.