0

I have set cookie up in my code, but when I try to fetch the cookie value I get following error:

Notice: Undefined index: mailquery in D:\Dev\htdocs\index.php on line 24

I have no idea what to do here. This is on a blank website, using newest version of XAMPP.

What is going wrong?

My code:

<?php
include "sqlconnect.php";
$connect = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_query("CREATE DATABASE IF NOT EXISTS users;") or die(mysql_error());
mysql_query("USE users;") or die(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS userinfo(id int AUTO_INCREMENT NOT NULL,
                                                mail varchar(255) default 0 NOT NULL, 
                                                phone varchar(10) default 0 NOT NULL,
                                                PRIMARY KEY(id));") or die(mysql_error());
if($_REQUEST['extr'] == 0){
    mysql_query("INSERT INTO userinfo(mail, phone) VALUES(" . "'". $_REQUEST['x'] . "'" . ", " . $_REQUEST['y'] . ");") or die(mysql_error());
};
if($_REQUEST['extr'] == 1){
    $query = mysql_query("SELECT * FROM userinfo;");
    $result = mysql_fetch_row($query);
    setcookie("mailquery", $result[1] . $result[2], time() + 300, "/");
};

echo $_REQUEST['mailquery'];
?>
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 16 '15 at 17:55
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 16 '15 at 17:55
  • This is because you are trying to fetch undefined/null value in this line: `echo $_REQUEST['mailquery'];` – Maytham Fahmi Nov 16 '15 at 18:23
  • mailquery is the cookie though. I'm trying to do a website a chrome extension would send you to so it can get info it needs. – Nieprzyjaciel Nov 16 '15 at 18:28
  • OP had has tow issues, the main problem was the cookie but the second problem was with MySQL table, but that won't change the fact that the question for the cookie part was valid, I have edit the question for OP as I followed up with on this question with OP. – Maytham Fahmi Nov 17 '15 at 18:02

1 Answers1

1

To view your cookie value, you should use $_COOKIE['cookiename'].

Try to replace

echo $_REQUEST['mailquery'];

with

if(isset($_COOKIE['mailquery'])) {
    echo $_COOKIE['mailquery'];
}

PHP reference:

NOTE: Please pay attention to comments from @JayBlanchard. If you can, you should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really not hard.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • It's still not working. This time it just returns nothing. – Nieprzyjaciel Nov 16 '15 at 18:48
  • this is because you `setcookie("mailquery", $result[1] . $result[2], time() + 300, "/");` is not excuted for some reason, if you just copy this line `setcookie("mailquery", 1 . 2, time() + 300, "/");` out of the if statement for testing you will find out it works and it should show 12 on your browser, you have to check your if statement. this is another issue then you question – Maytham Fahmi Nov 16 '15 at 18:51
  • extr is a variable put into the url, like localhost/index.php?extr=1 – Nieprzyjaciel Nov 16 '15 at 19:08
  • I tried testing the setcookie() thing outside the script and it still doesn't work. – Nieprzyjaciel Nov 16 '15 at 19:10
  • Chrome and IE still returns the error. – Nieprzyjaciel Nov 16 '15 at 19:13
  • Oh my god. I'm an idiot. The table, in the database, was empty. Sorry for wasting your time and thanks for your help! – Nieprzyjaciel Nov 16 '15 at 19:26