İçeriğe geç

PHP ile Sayfalar Arası Bilgi Gönderme

Using Forms:

This is one of the most common methods for passing data between pages. Data is collected from a user through an HTML form and then processed with PHP. For example, data can be sent using either the POST or GET method.

form.html
<form action="receiver.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form>
receiver.php
<?php
$name = $_POST['name'];
echo "Hello, $name!";
?>

Using URL Parameters:

URL parameters can be used to pass data from one page to another. This is done using the GET method.

sender.php
<?php
$name = "John";
$age = 25;
$url = "receiver.php?name=$name&age=$age";
header("Location: $url");
exit;
?>
receiver.php
<?php
$name = $_GET['name'];
$age = $_GET['age'];
echo "Hello, $name! Your age is: $age";
?>

Using Session Variables:

Session variables are used to store data that should be available across multiple pages during a user’s session. Once set, session variables can be accessed from any page.

sender.php
<?php
session_start();
$_SESSION['name'] = "Alice";
$_SESSION['age'] = 30;
header("Location: receiver.php");
exit;
?>
receiver.php
<?php
session_start();
$name = $_SESSION['name'];
$age = $_SESSION['age'];
echo "Hello, $name! Your age is: $age";
?>

Using Cookies:

Cookies are small pieces of data stored in the client’s browser. They can be set using the setcookie() function in PHP and accessed via the $_COOKIE superglobal.

sender.php
<?php
$name = "Sarah";
$age = 35;
setcookie("user_name", $name, time() + (86400 * 30), "/"); // Set cookie
setcookie("user_age", $age, time() + (86400 * 30), "/"); // Set cookie
header("Location: receiver.php");
exit;
?>
receiver.php
<?php
$name = $_COOKIE['user_name'];
$age = $_COOKIE['user_age'];
echo "Hello, $name! Your age is: $age";
?>

These methods can be chosen based on the specific requirements of the project and the desired level of security. Each method has its own advantages and disadvantages, and the choice of method depends on factors such as sensitivity of the data and security considerations.