Changing site color via GET parameter

Asked

Viewed 532 times

1

First of all I wanted to inform you that I am beginner in programming, I am learning by myself.

I have a question, I don’t want hand-kissed codes or anything, just an explanation so I can put it into practice.

I was browsing 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 this is done?

http://redeblast.com/player?color=FFCC00

3 answers

2


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

Url: https://www.example.com/? foo=bar

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

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

Url: https://www.example.com/? font-Weight=bold&bg=F00&color=FFF

PHP 5.6 or less

<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 later

<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 the comparison operators or null coalescing Operator you set a default color or other feature if the site does not receive these parameters.

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

2

This is done by taking the URL parameter with $_GET['color'] and assigning 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 where you want, for example as the background color of a div:

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

As every hexadecimal color is prefixed with #, just add the captured value of the URL and add the symbol # before the value.

-1

Just to complement the answer:

PHP has other variables with similar uses, the $_GET passes data through the url, the $_POST passes this "hidden" data (both are deleted when switching pages, for example, if you are on 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 will no longer exist, unless you forward it again) and the $_SESSION which creates a session with the data that will be saved until deleted (via code) or user exits 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"] finds the value of the get array at the position that has the example name

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

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

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

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

$_SESSION["exemplo"] = "valor do exemplo";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.