Cabeçalho em PHP para caracteres especiais

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>Exemplo de acentos UTF-8 áéíóú ãõ</p>
</body>
</html>

Url Decode

Arquivo: teste.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url Javascript</title>
</head>
<body>
    <script language="javascript">
        function Encode(){
            teste = document.form1.teste1.value;
            teste = encodeURIComponent(teste);
            alert("Encode: encodeURIComponent = "+teste);
            teste = decodeURIComponent(teste);
            alert("Decode: decodeURIComponent = "+teste);
        }
        function Link(){
            teste = document.form1.teste1.value;
            location.href = "http://localhost/teste/index.php?teste=" + encodeURIComponent(teste);
        }
    </script>
    <form name="form1">
    <input name="teste1" type="text" Value="João, freqüêcia, átomo, @" />
    <input type="button" onClick="Encode()" value="Executar!">
    <br />
    <a href="javascript: Link()">Link</a>
</form>
</body>
</html>
Arquivo: http://localhost/teste/index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url PHP</title>
</head>
<body>
    <p>
        <font color="red">urldecode: João, freqüêcia, átomo, @</font><br />
        <?php
            $teste = $_GET['teste'];
            $teste2 = $teste;
            $teste = urldecode($teste);
            echo $teste ."<br>\n";
        ?>
        <font color="red">htmlentities: Jo&amp;atilde;o, freq&amp;uuml;&amp;ecirc;ncia, &amp;aacute;tomo, @</font><br />
        <?php
            $teste = htmlentities($teste);
            echo $teste ."<br>\n";
        ?>
    </p>
</body>
</html>

Formulários

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Formu&aacute;rio</title>
</head>
<body>
<form action="users.php" method="post">
$nome: <input type="text" name="nome" value="Alexandre"><br>
$idade: <input type="text" name="idade" value="58"><br>
$profissao: <input type="text" name="profissao" value="Vendedor">
<button type="submit">Enviar</button>
</body>
</html>
Arquivo: users.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>teste</title>
</head>
<body>
<?php
$nome = $_POST['nome'];
echo "Nome: ".$nome."<br>";
$idade = $_POST['idade'];
echo "Idade: ".$idade."<br>";
$profissao = $_POST['profissao'];
echo "Profiss&atilde;o: ".$profissao."<br>";
?>
</body>
</html>

Url

www.meusite.com/users.php?nome=Alexandre&idade=58&profissao=Vendedor

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url</title>
</head>
<body>
    <p><a href="users.php?nome=Alexandre&idade=58&profissao=Vendedor">users.php?nome=Alexandre&idade=58&profissao=Vendedor</a></p>
    <p>&nbsp;</p>
</body>
</html>
Arquivo: users.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>teste</title>
</head>
<body>
    <?php
        $nome = $_GET['nome'];
        echo "Nome: ".$nome."<br>";
        $idade = $_GET['idade'];
        echo "Idade: ".$idade."<br>";
        $profissao = $_GET['profissao'];
        echo "Profiss&atilde;o: ".$profissao."<br>";
?>
</body>
</html>

htaccess

Url Amigável

Exemplos de Url Amigáveis tipo twitter
.
www.meusite.com/jorgeluiz
.
www.meusite.com/flavia
.
www.meusite.com/janaina
.
O que vai aconter é... quando digitar www.meusite.com/usuario vai redirecionar para www.meusite.com/users.php?id=usuario, lá vc pega o usuário como $_GET['usuario']... mas se digitar www.meusite.com/images ou /css ou /js, estas pastas são ignoradas pelo RewriteRule... no caso no RewriteCond coloque todas as pastas que serão visíveis aos usuários...

Url Amigavel (Exemplo 1)

www.meusite.com/teste

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url Amigavel</title>
</head>
<body>
    <p><a href="teste">teste</a></p>
    <p>&nbsp;</p>
</body>
</html>
Arquivo: .htaccess
RewriteEngine on
RewriteCond $1 !(images|css|js) [NC]
RewriteRule ^([a-z0-9]+)$ users.php?id=$1 [NC]
Arquivo: users.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>teste</title>
</head>
<body>
    <?php
        $variavel1 = $_GET['id'];
        echo $variavel1."<br>";
    ?>
</body>
</html>

Url Amigavel (Exemplo 2)

www.meusite.com/Alexandre/58/Vendedor

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url Amigavel</title>
</head>
<body>
    <p><a href="Alexandre/58/Vendedor">Alexandre/58/Vendedor</a></p>
    <p>&nbsp;</p>
</body>
</html>
Arquivo: .htaccess
RewriteEngine on
RewriteCond $1 !(images|css|js) [NC]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)$ users.php?nome=$1&idade=$2&profissao=$3 [NC]
Arquivo: users.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>teste</title>
</head>
<body>
    <?php
        $nome = $_GET['nome'];
        echo "Nome: ".$nome."<br>";
        $idade = $_GET['idade'];
        echo "Idade: ".$idade."<br>";
        $profissao = $_GET['profissao'];
        echo "Profiss&atilde;o: ".$profissao."<br>";
?>
</body>
</html>

Url Amigavel (Exemplo 3)

www.meusite.com/nome/Alexandre/idade/58/profissao/Vendedor

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Url Amigavel</title>
</head>
<body>
    <p><a href="nome/Alexandre/idade/58/profissao/Vendedor">nome/Alexandre/idade/58/profissao/Vendedor</a></p>
    <p>&nbsp;</p>
</body>
</html>
Arquivo: .htaccess
RewriteEngine on
RewriteCond $1 !(images|css|js) [NC]
RewriteRule ^nome/([a-z0-9]+)/idade/([a-z0-9]+)/profissao/([a-z0-9]+)$ users.php?nome=$1&idade=$2&profissao=$3 [NC]
Arquivo: users.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>teste</title>
</head>
<body>
    <?php
        $nome = $_GET['nome'];
        echo "Nome: ".$nome."<br>";
        $idade = $_GET['idade'];
        echo "Idade: ".$idade."<br>";
        $profissao = $_GET['profissao'];
        echo "Profiss&atilde;o: ".$profissao."<br>";
    ?>
</body>
</html>

Como gerar uuid em PHP com letras e números aleatórios

Gerador de código em PHP não existe! Deve ser criado! Útil pra criar urls aleatórios

Exemplo:

www.meusite.com/videos.php?v=73c5f7ad-2081-4f47-85bc-debaa0b18ea9

Esse indentificador gera código com letras e números aleatórios.

Exemplo:

228f99fd-c47d-4520-9144-0232473ae7fc

Arquivo: index.html
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<?php
    function uuid(){
        $data = random_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    echo uuid();
?>
</body>
</html>

Convertendo Números em Letras

Arquivo index.html
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<?php
function xid($y){
$x = array();
$x[0] = "-";
$x[1] = "1";
$x[2] = "2";
$x[3] = "3";
$x[4] = "4";
$x[5] = "5";
$x[6] = "6";
$x[7] = "7";
$x[8] = "8";
$x[9] = "9";
$x[10] = "a";
$x[11] = "b";
$x[12] = "c";
$x[13] = "d";
$x[14] = "e";
$x[15] = "f";
$x[16] = "g";
$x[17] = "h";
$x[18] = "i";
$x[19] = "j";
$x[20] = "k";
$x[21] = "l";
$x[22] = "m";
$x[23] = "n";
$x[24] = "o";
$x[25] = "p";
$x[26] = "q";
$x[27] = "r";
$x[28] = "s";
$x[29] = "t";
$x[30] = "u";
$x[31] = "v";
$x[32] = "w";
$x[33] = "x";
$x[34] = "y";
$x[35] = "z";
$x[36] = "A";
$x[37] = "B";
$x[38] = "C";
$x[39] = "D";
$x[40] = "E";
$x[41] = "F";
$x[42] = "G";
$x[43] = "H";
$x[44] = "I";
$x[45] = "J";
$x[46] = "K";
$x[47] = "L";
$x[48] = "M";
$x[49] = "N";
$x[50] = "O";
$x[51] = "P";
$x[52] = "Q";
$x[53] = "R";
$x[54] = "S";
$x[55] = "T";
$x[56] = "Y";
$x[57] = "V";
$x[58] = "W";
$x[59] = "X";
$x[60] = "Y";
$x[61] = "Z";
$x[62] = "_";

$k = $y;
$z = 0;
$s = "";
while(true){
    if($k > 62){
        $z = $k % 62;
        $s = $x[$z] . $s;
        $k = floor($k / 62);
    } else {
        $z = $k % 62;
        $s = $x[$z] . $s;
        break;
    }
}
return $s;
}

echo xid(54453225534443532);
?>
</body>
</html>

Convertendo números em letras inserindo uma data e horas mais um número aleatório

Arquivo: index.html
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<?php
function xid($y){
$x = array();
$x[0] = "-";
$x[1] = "1";
$x[2] = "2";
$x[3] = "3";
$x[4] = "4";
$x[5] = "5";
$x[6] = "6";
$x[7] = "7";
$x[8] = "8";
$x[9] = "9";
$x[10] = "a";
$x[11] = "b";
$x[12] = "c";
$x[13] = "d";
$x[14] = "e";
$x[15] = "f";
$x[16] = "g";
$x[17] = "h";
$x[18] = "i";
$x[19] = "j";
$x[20] = "k";
$x[21] = "l";
$x[22] = "m";
$x[23] = "n";
$x[24] = "o";
$x[25] = "p";
$x[26] = "q";
$x[27] = "r";
$x[28] = "s";
$x[29] = "t";
$x[30] = "u";
$x[31] = "v";
$x[32] = "w";
$x[33] = "x";
$x[34] = "y";
$x[35] = "z";
$x[36] = "A";
$x[37] = "B";
$x[38] = "C";
$x[39] = "D";
$x[40] = "E";
$x[41] = "F";
$x[42] = "G";
$x[43] = "H";
$x[44] = "I";
$x[45] = "J";
$x[46] = "K";
$x[47] = "L";
$x[48] = "M";
$x[49] = "N";
$x[50] = "O";
$x[51] = "P";
$x[52] = "Q";
$x[53] = "R";
$x[54] = "S";
$x[55] = "T";
$x[56] = "Y";
$x[57] = "V";
$x[58] = "W";
$x[59] = "X";
$x[60] = "Y";
$x[61] = "Z";
$x[62] = "_";

$k = $y;
$z = 0;
$s = "";
while(true){
    if($k > 62){
        $z = $k % 62;
        $s = $x[$z] . $s;
        $k = floor($k / 62);
    } else {
        $z = $k % 62;
        $s = $x[$z] . $s;
        break;
    }
}
return $s;
}
$x = date('Y') . date('n')+1 . date('d') . date('G') . date('i') . date('s') . mt_rand(0, 10000);
echo $x ;
echo "<br>\n";
echo xid($x);
?>
</body>
</html>

Cookies

Cookies Temporizados

Arquivo: cookie_1.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<?php setcookie("idade", "21", time() + 172800); // Cria o cookie com validade de 2 dias (60*60*24*2 = 172800) ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coockie 01</title>
</head>
<body>
    Enviando um Cookie<br><br>
    <a href="cookie_2.php">Enviar</a>
</body>
</html>
Arquivo: cookie_2.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cookie 02</title>
</head>
<body>
    Abrindo o Cookie<br><br>
    <?php
        if (isset($_COOKIE["idade"])) {
            echo "O valor do cookie é: <font color='#FF0000'>" . $_COOKIE["idade"] . "</font>";
        } else {
            echo "Cookie ainda não está disponível.";
        }
    ?>
    <br><br><a href="cookie_1.php">Voltar</a>
    <br><br>Fechando o cookie
</body>
</html>
<?php setcookie("idade", "", time() - 3600); // Apaga o cookie ?>

Cookies Fixos

Definidos por 1 ano

Arquivo: index.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<?php
    // Definindo cookie fixo com validade de 1 ano
    setcookie("idade", "21", time() + (365 * 24 * 60 * 60));
// 1 ano
?>
<!DOCTYPE html>
    <html lang="pt-br">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cookie Fixo</title>
</head>
<body>
    Abrindo o Cookie<br><br>
    <?php
    if (isset($_COOKIE["idade"])) {
        echo "O valor do cookie é: <font color='#FF0000'>" . $_COOKIE["idade"] . "</font>";
    } else {
        echo "Cookie ainda não está disponível.";
    }
    ?>
</body>
</html>