PHP cookies to pass variables between two pages [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I am aware that I can use sessions and GET/ POST method but I would like to achieve this using cookies. My code on page1.php is :
$_COOKIE['varname'] = $id;
and on page2.php is:
$id = $_COOKIE['varname'];
I get the following notice on my browser: Undefined index $id
What is the problem with my code?

Try using setcookie('varname', $id) then
if (isset($_COOKIE['varname']){ echo $_COOKIE['varname']; }

To set a cookie you need to use setcookie(). And it has to be done prior to any output.
setcookie("mycookie", "myvalue" , $validtime); // validtime is a integer.

Related

PHP Notice: Undefined index section? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 10 months ago.
I use the section, but this error occurs, in fact, it enters the site and everything is done correctly, but this error occurs behind the scenes. What is the solution?
PHP Notice: Undefined index: walletaddrass in /home/decent/public_html/user/presale.php on line 5
$id_wallet = $_SESSION["walletaddrass"];
The quantification is done as follows:
$_SESSION["walletaddrass"] = $id_wallet;
setcookie('logged-in','true', time() + 286400 , '/');
Please post the whole file where you are getting this error next time.
The issue is because you are trying to use $_SESSION["walletaddrass"] when it's not set, as you are setting it in the line below it.
You must first check if the session variable is set as follows:
if (isset($_SESSION['walletadrass'])) {
$id_wallet = $_SESSION["walletaddrass"];
$_SESSION["walletaddrass"] = $id_wallet; setcookie('logged-in','true', time() + 286400 , '/');
}

How to get rid of undefined index in this situation [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP check value against multiple values with OR-operator
(3 answers)
Closed 4 years ago.
Hi,
I have this code:
if($_GET['s']=="page1" || $_GET['s']=="page2" || $_GET['s']=="page3") {
dosomething();
}
and I get this error: : Undefined index: s in
Which I can dismiss only by adding this line:
$_GET['s']="";
but then this wont execute the code correctly since $_GET['s'] is not supposed to have any initial value. How do I fix this other than disabling the notices and errors?
Thank you.
You can check your $_GET['s']
if(isset($_GET['s'])) {
// your code goes here...
}
isset() is used to check if the index exists.

I'm getting syntax error when trying to send password in form [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I'm very new at php. I get these errors when I write these codes.
check to see if islem was passed in before trying to access it
if (isset($_POST['islem']) {
// your code here
}
Otherwise you won't be able to load the page up initially in order to submit the form. Also notice that I used _POST here, this is because you used the POST method in your form.
You are using the method POST in the form and getting the variables with GET
Either Try method : GET or try $_POST["islem"];

PHP URI values showing up as undefined [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
What I want to do is echo the value of $test on my page, through the uri. I know I'm doing something wrong, but I can't figure out what.
Code:
<?php echo $test?>
URL:
localhost/testMap/test.php?test=hallo
You can get the value of url with a get, and store it in the variable $test and then echo that out.
$test = $_GET['test'];

i am i getting notice: undefined pss [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
here is my php. the session ps comes from a another php page. it comes successfully. but i am getting undefined when i try to use it on the following code to connect to my db.
<?php
$pss = "";
session_start();
if(isset($_SESSION['ps'])){
$pss = $_SESSION['ps'];
}
echo $pss;
$connect = mysqli_connect("localhost","root","","erthiph_asksheikh851821217");
mysqli_query($connect,"INSERT INTO `as_questions`(`Qid`, `M_class`, `S_class`, `Question`, `Answer`, `Doctor`, `Time`) VALUES
('','','','".$pss."','','',CURRENT_TIMESTAMP)");
?>
the above code works. i replaced '$_POST[postquestion]' with '".#pss."'
please check edit history to check edits.
change '$_POST[$pss]' to '".$pss."'

Categories