【问题标题】:PHP Undefined Index/ How do I 'define' html arrayPHP 未定义索引/我如何“定义”html 数组
【发布时间】:2013-01-24 22:13:36
【问题描述】:

您好,关于 PHP 的快速问题,我仍在学习为什么下面代码中的 $html[$firstname] 在服务器错误日志中作为未定义索引出现...... 快速回答会很棒......

您好,为了清楚起见,这是所有代码,如果 PDO 的代码足够好并且不需要转义,请告诉我,我猜在输出数据之前不需要 html 实体数据库通过php到浏览器。

<?php

$firstname = "";
$lastname = "";
$username = "";
$email = "";
$password = "";
$confirm_password = "";
$_POST['firstname'] = $firstname;
$_POST['lastname'] = $lastname;
$_POST['username'] = $username;
$_POST['email'] = $email;
$_POST['password'] = $password;
$_POST['confirm_password'] = $confirm_password;
$clean =  array();
if(ctype_alnum($firstname)){
  $clean[$firstname] = $firstname;
};
if(ctype_alnum($lastname)){
  $clean[$lastname] = $lastname;
};
if(ctype_alnum($username)){
  $clean[$username] = $username;
};

if(isset($email)){
  filter_var($email, FILTER_SANITIZE_EMAIL);
};
//initialize an array for escaped data
$html = array();
//escape the filtered data
$html[$firstname] = htmlentities($clean[$firstname], ENT_QUOTES, 'UTF-8');
$html[$lastname] = htmlentities($clean[$lastname], ENT_QUOTES, 'UTF-8');
$html[$username] = htmlentities($clean[$username], ENT_QUOTES, 'UTF-8');
$html[$email] = htmlentities($email, ENT_QUOTES, 'UTF-8');
$html[$password] = htmlentities($password, ENT_QUOTES, 'UTF-8');
$html[$confirm_password] = htmlentities($confirm_password, ENT_QUOTES, 'UTF-8');
//
//write function to generate random salt for every password, + bcrypt allpasswords, then store in db


$salt = substr(str_replace('+', '.', base64_encode(pack('N4', mt_rand(),  mt_rand(), mt_rand(), mt_rand()))), 0, 22);
$hash = crypt($html[$password], '$2a$10$'.$salt.'$');


$currentPassword = '$2a$15$Ku2hb./9aA71tPo/E015h.LsNjXrZe8pyRwXOCpSnGb0nPZuxeZP2';
$checkPassword = $password;
if(crypt($checkPassword, $currentPassword) === $currentPassword){
    echo 'You are in!';
}else{
    echo 'You entered the wrong password';
}

// store everything in the database execute prepare, then send back the email verification, do not send
//new password to email, and don't send forgotten password to email, just get them to remember it and click the link'
//connect to the database
$user = "*****";
$dbpassword = "****";
$db = new PDO('mysql:host=localhost;dbname=_virtualpiersclose', $user, $dbpassword);
$statement = $db->prepare("INSERT INTO users (firstname, lastname, username, email, password)
    VALUES (:firstname, :lastname, :username, :email, :password)");
$statement->bindParam(':firstname', $html[$firstname]);
$statement->bindParam(':lastname', $html[$lastname]);
$statement->bindParam(':username', $html[$username]);
$statement->bindParam(':email', $html[$email]);
$statement->bindParam(':password',$html[$password]);

$statement->execute();

$db = NULL;

?>

【问题讨论】:

  • 您发布的代码并不表明 $html[$firstname] 曾经未定义。你确定这是相关的代码吗? PHP 通常会在此类消息中附带行号。你确定错误信息是关于$html[$firstname]吗?
  • 你到底想完成什么?您是否试图逃避/清理您的用户输入?因为如果你这样做,你就会走错路
  • 请修正代码的缩进。
  • 嗨,是的,我昨晚正在研究 santization,发现 my_sql_real_escape_string 已被弃用,所以我决定主要使用 PDO 准备语句来避免 sql 注入,这足以使用 prepare/bindParam/ 进行转义吗执行还是应该增加一层转义。我认为使用 html 实体可以防止任何 XSS 进入,然后也可能通过数据库退出......
  • @AntPower 它是 mysql_real_escape_string() ,是的,如果你使用 PDO(并正确使用它)你会没事的。但是目前在您的脚本中没有数据库连接,因此我们无法为您提供帮助。

标签: php arrays forms


【解决方案1】:

我想你想要的是:

$html['firstname']

话虽如此,您的代码有点疯狂,没有多大意义。以下是一些访问模式:

$ar = array(
    'firstname' => 'Joe',
    'hello' => 'Hi there!'
);

$n = 'firstname';  // assign the string 'firstname' to $n
$x = $ar['firstname'];  // $x becomes 'Joe'
$x = $ar[$n]; // also $x becomes 'Joe', because $n is 'firstname'
$x = $ar[$firstname]; // doesn't return anything, because the variable $firstname is not assigned.  Will trigger a warning, too.
$firstname = 'hello'; // assign 'hello' to $firstname
$x = $ar[$firstname]; // $x becomes 'Hi there!'
$ar['hello'] = 'Good Bye.'; // Change $ar['hello']
$ar[$firstname] = 'So long!'; // Also changes $ar['hello']

你的赋值语句也倒过来了。等号左侧的变量接收来自右侧的值。我认为您希望将$_POST['firstname'] 的内容保存在变量$firstname 中。这里有几行可能需要修复:

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
// ... etc ...
if(ctype_alnum($firstname)){
    $clean['firstname'] = $firstname;
};

【讨论】:

  • $html['firstname'] 的使用方式绝不会引发有关未定义索引的消息。
  • $firstname 是一个空字符串。语句$html[''] 将返回未定义索引的警告。
  • 不,不会。空字符串是完全合法的数组索引。
  • 是的,但如果变量$html[''] 从未设置过,那么它是一个无效索引。无论如何,所有这些都是无关紧要的:OP 的代码是一团糟,因为他显然是 PHP 新手,需要一点帮助。我仍然认为他的基本数组访问模式是错误的。
  • 写入未定义索引处的数组会自动创建该索引条目。 Morover 写入$array[$index] 如果$array 未定义,则会自动创建一个数组$array 和适当的数组条目。而且问题中的代码不是从$html['']读取的。
猜你喜欢
  • 2015-12-15
  • 2011-08-15
  • 2012-12-08
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多