Changing site color via GET parameter

1

First of all I wanted to inform you that I'm a beginner in programming, I'm learning on my own.

I have a doubt, I do not want hand-coded kisses or anything, just an explanation so I can put it into practice.

I was surfing the internet and I entered a web-radio, I saw that in their player it is possible to change the color with a code in the URL. How is this done?

link

    
asked by anonymous 17.02.2018 / 05:57

3 answers

2

This is done using the global variable $ _GET . With this variable you can identify the parameters used in URL . Ex:

Url: link

var_dump( $_GET["foo"] ); //Output --> bar

This way you can pass any value, including you can use the value of this variable in a CSS

Url: link

PHP 5.6 or lower

<style>
    body {
        font-weight: <?php echo isset($_GET["font-weight"]) ? $_GET["font-weight"] : "normal" ?>,
        background: #<?php echo isset($_GET["bg"]) ? $_GET["bg"] : "000" ?>,
        color: #<?php echo isset($_GET["color"]) ? $_GET["color"] : "FFF" ?>,
    }
</style>

PHP 7.0 or higher

<style>
    body {
        font-weight: <?php echo $_GET["font-weight"] ?? "normal" ?>,
        background: #<?php echo $_GET["bg"] ?? "000" ?>,
        color: #<?php echo $_GET["color"] ?? "FFF" ?>,
    }
</style>
  

Tip: Using comparison operators or null coalescing operator you define a color or other standard feature if the site does not receive these parameters.

But be careful how and where you use those values. Some people use Cross-site scripting techniques for attacks.

    
17.02.2018 / 06:16
2

This is done by taking the URL parameter with $_GET['color'] and assigning it to a variable that will be used in PHP to set the color where you want. Example:

$cor = $_GET['color'];

The variable $cor can be applied to the HTML of the page wherever you want, for example, as the background color of a div :

<div style="background-color: #<?=$cor?>"></div>

Since all color in hexadecimal is prefixed with # , simply add the captured value of the URL and append the symbol # before the value.

    
17.02.2018 / 06:29
-1

Just to complement the answer:

PHP has other variables with similar uses, $_GET passes data through url, $_POST passes this "hidden" data (both are deleted when you change pages , for example, if you are in ex1.php and pass the data via GET or POST to ex2.php the data will be there, but when going to ex3.php this data did not exist anymore, unless you resend it again) and $_SESSION that creates a session with data that will be saved until deleted (via code) or user exiting the browser

These variables are global (can be accessed from any part of the site) and array type, so to get a GET, POST or SESSION you must pass a name, for example, $_GET["exemplo"] looks for the get array value in the position that has the name example

To send HTML data to PHP use the <form> tag specifying how you want to send the data in method (POST or GET), in input use name attribute to specify name when get this value for example:

<form action="pagina/de_destino.php" method="POST">
    <input type="text" name="exemplo">
</form>

In PHP use $_GET["exemplo"] to get the value sent by the user

SESSION needs to be created directly in php, for example:

$_SESSION["exemplo"] = "valor do exemplo";
    
17.02.2018 / 06:43