SuperGlobal Question

Her kan du få hjælp og stille spørgsmål til PHP!
Besvar
Crow
Rutineret
Rutineret
Indlæg: 64
Tilmeldt: 15. apr 2014, 11:24

SuperGlobal Question

Indlæg af Crow » 28. jan 2015, 10:08

I have been wondering for awhile regarding the usage of the superglobals and what you should/shouldn't do and what's best practice when using it.

here's a simple example where i depending on the sites url it changes $lang which can be used elsewhere or just skipped/replaced with $ccontent, the main objective is that depending on the url it will choose which .php file it will draw its content and variables from.

However i am warned by netbeans not to use superglobals in an array or similar but i still need to get the information and i need to use more superglobals information in the future so i would like to know how i can filter or use them better especially since i want to prevent vulnearabilities & errors cuased by simple misunderstandings so i hope someone can give me a how-to & why-not explanation ☺

Kode: Vælg alt

<?php    
switch ($_SERVER["REQUEST_URI"]) {
    case "/test/dk/.php":
        $Lang = "dk";
        break;
    case "/test/de.php":
        $Lang = "de";
        break;
    default:
        $Lang "en";
}

switch ($lang"]) {
    case "dk":
        $CContent = "DK-Content.php";
        break;
    case "de":
        $CContent = "DE-Content.php";
        break;
     case "en":
        $CContent = "EN-Content.php";
        break;
    default:
        $CContent "EN-Content.php";
}

include '$CContent';

?>
example of getting complete url with a function

Kode: Vælg alt

    function currentPageURL() {
        $curpageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $curpageURL.= "s";
        }
        $curpageURL.= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } 
        else {
            $curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $curpageURL;
    }
    $Current_URL = currentPageURL(); 

Crow
Rutineret
Rutineret
Indlæg: 64
Tilmeldt: 15. apr 2014, 11:24

Re: SuperGlobal Question

Indlæg af Crow » 29. jan 2015, 14:26

Here's another example with the same objective but i am still unsure of what the best approach is when it comes to superglobals like $_Server :?: so i would appreciate if someone could point out what not to and what to do or if i am doing it correctly since it works but i just dont know if it is safe & best practice or not :)

Kode: Vælg alt

<?php
    function currentPageURL() {
        $curpageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $curpageURL.= "s";
        }
        $curpageURL.= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } 
        else {
            $curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $curpageURL;
    }
switch (true) {
  case strstr($Current_URL,'dk'):
    $Content = "DKContent.php";
    break;
  case strstr($Current_URL,'de'):
    $Content = "DEContent.php";
    break;
    else 
    $Content = "ENContent.php";
        }
}

include '$Conent';
?>

Besvar