nethken nethken - 5 months ago 9
HTML Question

$_GET or $_SESSION for passing variable

Hello monsters of programming. I just want to ask a question about using

$_SESSION
and
$_GET
. When to use
$_GET
and
$_SESSION
? what is the best for passing variable? Im just new to php and html and i don't know what is the best practice. Can someone help me to understand both of them?

Here is the example of my code. I used
$_SESSION
for passing the variable
$newsid;


here is the edit.php

<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];

?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type='hidden' name="news_id" value="<?php echo $newsid;?>">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
header('Location: edit2.php');
}
?>


here is the edit2.php

<?php
session_start();

$id = $_SESSION['news_id'];
include_once('connection.php');
$sql = "SELECT * FROM news WHERE news_id = '$id'";
$result = mysqli_query($con,$sql);


while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
}

?>
<!DOCTYPE HTML>
<html>
<head>

</head>
<body>


<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $title;?>"/><br>
Date<input type ="text" name="date" value="<?php echo $date;?>" /><br>
Content<textarea name="content"><?php echo $content;?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $newsimage;?>" alt="your image" style="width:200px; height:140px;"/>

</form>
<hr>


<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

Answer

$_GET is for parameters that are needed during that specific request (or can be easily carried over to other pages), e.g.:

  • item IDs
  • current page (pagination)
  • user's profile name
  • ...

$_SESSION is for data that needs to be persisted across multiple requests, e.g.:

  • current user's ID
  • shopping carts
  • list filters
  • ...

You should use the one that better suits your use case.

That being said, I'd consider storing news_id in the session a bad thing. What if I want to edit multiple items and open multiple browser tabs? I'll end up overwriting my data. Just because you can use sessions doesn't mean you should.