Can I use $_GET with $_SESSION?

Asked

Viewed 170 times

1

I have this PHP code working,

<?php
include "../conexao.php";
$codigo = $_POST['codigo'];
$nome_marca = $_POST['nome_marca'];
$query = mysql_query("SELECT * FROM marca order by nome_marca")or die(mysql_error());
while($res = mysql_fetch_array($query)){
?>
<ul>
    <li><a href="prod_index_marca.php?codmarca=<?php echo $res['codigo'];?>"><?php echo $res['nome_marca'];?></a></li>
<?php
}
?>
</ul>      

and which directs the href to the page according to the code referring to the selected brand.

And the landing page is with this PHP code

<?php
include "../conexao.php";
$codmarca = $_GET['codmarca'];
$query = mysql_query("SELECT * FROM produto WHERE codmarca = '$codmarca'");
while($res = mysql_fetch_array($query)){

$codigo = $_POST['codigo'];
$img01 = $_POST['img01'];
$descricao = $_POST['descricao'];
$titulo = $_POST['titulo'];
$codcategoria = $_POST['codcategoria'];
$codmarca = $_POST['codmarca'];
$preco = $_POST['preco'];
$pagseguro = $_POST['pagseguro'];
$titulo = $_POST['titulo'];
?>

that receives the data via $_GET.

My question is whether you can adapt these codes to work with $_SESSION?

And if so, friends can guide me how to convert them, or even where I can get information on the subject.

Here is my thanks to all, for the attention to my doubt.

  • 1

    remembering that Session is only between the same domain, and today it is not recommended to store a lot of data in Session

  • honestly, does it not... adapting code to get wrong is not a good practice!

1 answer

0

I assume you already know how a session works.

So you’ll practically change your $_POST for $_SESSION (post in this case will not work, as it is only used in Formularios Ubmit)

Remember that to use Sesssions, you must initialize with the function session_start(); always when reading or writing a variable in the session. Give preference to always use the first line of the code, because if there is a text output (or PHP notice or Warning error) will cause error in the script

follow the official documentation of session_start I assume you already know how a session works.

So you’ll practically change your $_POST for $_SESSION (post in this case will not work, as it is only used in Formularios Ubmit)

Remember that to use Sesssions, you must initialize with the function session_start(); always when reading or writing a variable in the session. Give preference to always use the first line of the code, because if there is a text output (or PHP notice or Warning error) will cause error in the script

follow the official documentation of session_start() http://php.net/manual/en/function.session-start.php

Browser other questions tagged

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