Skip to content

Instantly share code, notes, and snippets.

@mvnp
Created June 14, 2017 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvnp/d702c1a8bfc0c5627e991f6ddd506167 to your computer and use it in GitHub Desktop.
Save mvnp/d702c1a8bfc0c5627e991f6ddd506167 to your computer and use it in GitHub Desktop.
<?php
// VERIFICA SE A VARIÁVEL PROD FOI ENVIADA
if(!empty($_GET['prod'])){
// VERIFICA SE O CARRINHO JÁ FOI CRIADO, CASO NÃO, CRIA
if(!isset($_SESSION["carrinho"])){
// INICIANDO A SESSION
session_start();
// CRIANDO O CARRINHO
$_SESSION["carrinho"] = array();
}
// RECEBENDO O PRODUTO
$prod_id = $_GET['prod'];
// INSERINDO O PRODUTO NO CARRINHO
array_push($_SESSION["carrinho"], $prod_id);
}
// SE O CARRINHO EXISTE, MOSTRA O DUMP
if(isset($_SESSION['carrinho'])){
echo "<pre>";
print_r($_SESSION["carrinho"]);
echo "</pre>";
}
?>
<!DOCTYPE html>
<html lang="pt-br en">
<head>
<meta charset="UTF-8">
<title>Carrinho de compras</title>
</head>
<body>
<a href="index.php?prod=12">Produto 12</a><br>
<a href="index.php?prod=13">Produto 13</a><br>
<a href="index.php?prod=14">Produto 14</a><br>
<a href="index.php?prod=15">Produto 15</a><br>
<a href="index.php?prod=16">Produto 16</a><br>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment