if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
$value = $_POST['searcheditem'];
header("Location : anotherpage.php");
}
I use this code in my project, but when I am redirected to anotherpage.php I can't use $value. It is not equal to $_POST['searcheditem'] at that page.
What can I do?
You have 2 options in this case:
1. Use a query string
$value = $_POST['searcheditem'];
header("Location : anotherpage.php?myValue=".$value);
// then inside anotherpage.php
echo $_GET['myValue']; // make sure to sanitize this data
2. Use Sessions
$value = $_POST['searcheditem'];
$_SESSION['myValue'] = $value; // make sure to use session_start() at the top of the page
header("Location : anotherpage.php");
// then on the anotherpage.php page
// make sure you call session_start() at the top of this page too
echo $_SESSION['myValue']; // make sure to sanitize this too
You can do it in two ways, either can set it in session or can pass like query string
Query string method:
if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
$value = $_POST['searcheditem'];
header("Location : anotherpage.php?value=$value ");
}
Session Method:
if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
$value = $_POST['searcheditem'];
session_register("value"); // USE THIS ONLY IF YOUR PHP VERSION IS < 5.3.0
$_SESSION["value"] = $value;
header("Location : anotherpage.php");
}
Related
How I can create multiple get/post request in page URL?
Like this:
http://www.example.com/?goto=profile&id=11326
I already tried with this:
$doVariableprofile = isset($_GET['goto']) ? $_GET['goto'] : null;
if($doVariableprofile == 'profile') {
if(empty($_SESSION['logged'])) require_once('register.php');
}
how i can add more request?
now i have http://www.example.com/?goto=profile
i trying do this http://www.example.com/?goto=profile&id=1
$testt1 = isset($_GET['goto']) ? $_GET['goto']:null;
if($_GET['goto'] == 'profile?id=".$_GET['id']"'){
require_once('profile.php');
}
Doesn't work page when I add to profile?id="$_GET['id']"')
$goto = isset($_GET['goto']) ? $_GET['goto']:null;
$id = isset($_GET['id']) ? $_GET['id']:0;
if($goto == 'profile' && $id != 0){
require_once('profile.php');
}
you need to assign these values to variables, if you directly write $_GET['id'] in
'if condition' and those values are not available then you may get
"Notice: Undefined index: " error.
I believe this could be done like this
$url = "profile?id=".$_GET['id'];
if($_GET['goto'] == $url){
require_once('profile.php');
}
another thing I understood this could be done like this
if(isset($_GET['goto']) && $_GET['goto']=="profile"){
if(isset($_GET['id'] || $_GET['id']==''){
header("Location: profile.php");
}
}
I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.
It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks
Are you looking for something like this. If so, it should work for your case:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers will apply only after it send to client. If you want immediately redirect, you can put exit(0) after header(...) in this case you are stop executing of the script and will send current headers to the browser which will redirect you.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
The problem is that you are comparing the "value" of isset (the result) with the value of your GET parameter, $id:
if(isset($_COOKIE['logged_in']) == $id)
What this says is "determine if $_COOKIE['logged_in'] is set and compare that determination to $id". PHP will evaluate isset, which returns true or false (as it says in the documentation), and compare that true or false to the other side of the expression (==), meaning $id, which will never match given your examples. If you query "random.com/test.php?id=true" (or false) that might do what you are looking for.
The line you have does not mean "determine if $_COOKIE['logged_in'] is set and compare the value of $_COOKIE['logged_in'] to the value of $id", which I believe is what you are looking for. In that case, what you want to do is first check that $_COOKIE['logged_in'] is set and then check that the value of $_COOKIE['logged_in'] matches $id, like so:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
If that doesn't make sense, here is a really explicit version that might be clearer as to what is actually going on:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
Hope that helps.
you should add another condition.
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
or use this script
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}
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'];
}
I am creating a checkout page that requires the client to fill out his personal information as well as his credit card details (this part using stripe).
I was wondering, what is the best way to check whether the fields are filled up or not? Shall I do it in the processingPayment.php that $_POSTs the fields and processes payment, and in case the fields were not filled, I would redirect back to checkout?
Or is it a better idea to use js to check on the spot before submitting the form?
if in the processing page, I would try something like this:
if (empty($firsName) || empty($lastName) || empty($address) || empty ($city) || empty ($state) || empty($zip))
{
header('Location: checkout.php');
}
But I would need to re-send the values that were entered so the checkout page receives them and the user doesn't have to re-fill every field again...
Something like this?
foreach($_POST as $key=>$val) {
if( empty($val) ) {
echo "$key is empty";
}
}
The best method with PHP is to have an array of possible arguments:
$array = array('firstName', 'lastName');
foreach($array as $val) {
if( empty($_POST[$val]) ) {
echo "$val is empty";
}
}
Otherwise, client side validation works too, but can always be disabled. To be completely safe, use both client and server side.
You can use the session to store the entered data, but you would need to check each value separately:
PHP
<?php
session_start();
foreach ($_POST as $key => $value) {
if (strlen(trim($value)) <= 0) { //You could replace '0'
$_SESSION[$key] = $value;
}
}
?>
FORM
<form>
First name: <input type="text" value="<?php $_SESSION['firstName'] ? $_SESSION['firstName'] : ''; ?>" placeholder="First Name" />
....
</form>
The $_SESSION['firstName'] ? $_SESSION['firstName'] : ''; is the same as
if ($_SESSION['firstName']) return $_SESSION['firstName']
else return '';
it is more readable in the HTML(View) that the full if statement
$var = isset($_POST['field']) ? $_POST['field'] : '';
$var2 = isset($_POST['field2']) ? $_POST['field2'] : '';
// and so on
if( empty($var) || empty($var2) )
{
//it's empty
}
What I'm doing is, if I haven't got an ID in either $_POST or $_SESSION then redirecting. Preference is given to $_POST. So I have this:
$bool = 0;
if (isset($_POST['id'])) {
$bool = 1;
} elseif (isset($_SESSION['id'])) {
$bool = 1;
}
if (!$bool) {
...//redirect
}
Is there a quicker way to write this, APART from just removing the braces?
if(!( isset($_POST['id']) || isset($_SESSION['id']) ))
redirect();
(not sure if I understand how what's given to $_POST is preference).
You could just do:
$has_id = isset($_POST['id']) || isset($_SESSION['id']);
if (!$has_id) {
// redirect
}
(I'd recommend you to give your variables more descriptive names than just $bool.)
Although if you aren't using the variable for anything else, you could just do:
if (!isset($_POST['id']) && !isset($_SESSION['id'])) {
// redirect
}
if (isset($_POST['id']) || isset($_SESSION['id'])) {
$bool = 1;
}
This will do it, simples
$bool = (isset($_POST['id']) || isset($_SESSION['id'])) ? 1 : 0; // if isset, 1
($bool == 1?header(Location: www.whatever.com):null;
Using Conditional Operator, you can achieve this in one line statement
Example:
c = (a == b) ? d : e;