My PHP page can receive the same data from two differents pages, the first one send it using GET, and the second with sessions. How can I make this thing work ?
//$var = empty;
//$_GET['id'] = empty;
//User come from page1.php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$var = $_GET['data'];
}
else {
//User come from page2.php
$var = $_SESSION['data'];
}
Try this:
if (array_key_exists('data', $_GET)) {
$var = $_GET['data'];
} else {
$var = $_SESSION['data'];
}
If you have two pages, page1.php
and page2.php
and you want to know at first hand which page it is, and what is the ?data=
value... then this will do the trick.
<?php
$data = (isset($_GET['data']) && !empty($_GET['data'])) ? $_GET['data'] : '';
if($_SERVER['SCRIPT_NAME'] === 'page1.php'){
$var = $data;
}else if($_SERVER['SCRIPT_NAME'] === 'page2.php'){
$var = $_SESSION['data'];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.