How to store values in an array?

Now this code:
<?php
if(isset($_COOKIE["user_ip"])){
if(isset($_COOKIE["user_password"])){
 if($_COOKIE["user_ip"] == $_SERVER['REMOTE_ADDR']){
 $password = $_COOKIE["user_password"];
 $ip = $_COOKIE["user_ip"];
include('config.php');
 mysql_connect($db_server, $db_user, $db_password);
mysql_set_charset('utf8');
mysql_select_db($db_table);
 $query = mysql_query("SELECT * FROM users WHERE password='$password' AND ip='$ip'");
if(!$query){
 header('Location: login.php');
 } else {
 $user = mysql_fetch_array($query, MYSQL_ASSOC);
 echo 'Hello '.$user['name'].' '.$user['family'].";
}
 } else {header('Location: login.php');}
 } else {header('Location: login.php');}
} else {header('Location: login.php');}
echo 'Hello '.$user['name'].' '.$user['family'].";
?>

I want typing $user['name'] is output name from the array, but it is not displayed, what's the problem? Or maybe advise some other function to use?
September 26th 19 at 07:13
1 answer
September 26th 19 at 07:15
Solution
1) to Explain why it's wrong to do?
if () {
 if () {
 if () {
 if () {
...
 } else {
 $var = ...
}
}
}
}
echo $var;

2) do Not store the password in the cookie! Especially in the clear!
3) Read about DRY.
4) Use pdo, mysql_ functions are obsolete and insecure.
Explain, I'm not very good at PHP, as You know. - Skyla73 commented on September 26th 19 at 07:18
echo $var needs to be done in the same place where the variable is defined, or else in cases when at least one of the 4 conditions is not met, the code will not work. - kier commented on September 26th 19 at 07:21
apparently this is the reason why not excreted from the array. - Skyla73 commented on September 26th 19 at 07:24

Find more questions by tags MySQLPHP