【问题标题】:Undefined variable : function parameter (PHP)未定义变量:函数参数(PHP)
【发布时间】:2015-04-08 13:55:45
【问题描述】:

我是 PHP 新手,如果未定义用户名和密码,我的 php 表单验证会返回此错误。

注意:未定义变量:D:\hpsp\controller\loginvalidation.inc.php 第 64 行中的用户名

我使用 2 个函数(usernameValidation 和 passwordValidation)来检查 $_POST 输入是否正确,但我不知道是什么以及我必须在哪里放置正确的脚本,提前谢谢你。

<?php
session_start();
require_once('../model/pdo.inc.php');
// function for checking the username validation (not empty & Regex)
function usernameValidation($username) // Username as parameter
{
if ( !empty($_POST['username']) )
{
    $username = strtolower(htmlspecialchars($_POST['username'])); // username will be tranform to lowercase before regex matching

    if ( preg_match('#^[a-z0-9\.]{5,20}$#', $username) ) //  5 <= username lenght <= 20 in lowercase character to be valid
    {
        return true; // return true when the username is valid
    }
    else
    {
        echo "Invalid username, please re-try" ;
    }
}
else
{
    echo "Enter your username";
}
}

// function for checking the password validation (not empty & Regex)
function passwordValidation($password) // Password as parameter
{
if ( !empty($_POST['password']) )
{
    $password = htmlspecialchars($_POST['password']) ; // Protect the password

    if ( preg_match('#^[a-zA-Z0-9\.-_@$()]{6,10}$#', $password) ) // 6 <= password length <= 10 character to be valid
    {
        return true; // return true when password is valid
    }
    else 
    {
        echo "Invalid password, please re-try";   
    }
}
else
{

    echo "Enter your password";    
}
}


if ( usernameValidation($username) == true AND passwordValidation($password) == true )
{
// PDO Query (SELECT ...)
}

【问题讨论】:

  • 要将您的参数设置为可选,您需要这样设置:functionName($var='default')
  • $username 您在脚本中哪里定义了这个变量? :)
  • 尝试回显 $_POST['username'] 并检查它是否有价值?
  • 您在 usernameValidation 中初始化 $username。因为这是一个函数,所以不能在该函数之外调用 $username 变量。您需要在函数 usernameValidation() 之外定义 $username 以使其工作。
  • 使用 if ( usernameValidation($_POST['username']) == true AND passwordValidation($POST['password']) == true )

标签: php function post undefined function-parameter


【解决方案1】:

我会做这样的事情(请注意,您永远不想回显电子邮件和密码的个别消息,以阻止黑客获取有关正确信息的信息:

session_start();
require_once('../model/pdo.inc.php');

//username and password will contain the posted resulte or FALSE
$username = usernameValidation();
$password = passwordValidation();
if (!$username OR !$password) {
    echo 'Invalid username or password!';
    die;
}
// PDO Query (SELECT ...)

// function for checking the username validation (not empty & Regex)
function usernameValidation() { // Username as parameter
    if (!empty($_POST['username'])) {
        $username = strtolower(htmlspecialchars($_POST['username'])); // username will be tranform to lowercase before regex matching

        if (preg_match('#^[a-z0-9\.]{5,20}$#', $username)) { //  5 <= username lenght <= 20 in lowercase character to be valid
            return $username; // return true when the username is valid
        }
    }
    return FALSE;
}

// function for checking the password validation (not empty & Regex)
function passwordValidation() { // Password as parameter
    if (!empty($_POST['password'])) {
        $password = htmlspecialchars($_POST['password']); // Protect the password

        if (preg_match('#^[a-zA-Z0-9\.-_@$()]{6,10}$#', $password)) { // 6 <= password length <= 10 character to be valid
            return $password; // return true when password is valid
        }
    }
    return FALSE;
}

【讨论】:

  • 你是对的 naw103 我专注于为用户提供正确的用户名或密码,但出于安全原因,我会考虑这一点。
  • 我可以问你一个问题,为什么我不应该给函数一个参数?
  • 您不需要将用户名传递给函数,因为您已经从这些函数中的 POST 获取数据,因此我看到函数的工作是为您获取用户名(以及执行验证等) )
【解决方案2】:

用参数定义你的函数

function usernameValidation(){ ... }

并称之为

if ( usernameValidation() == true AND passwordValidation() == true )

【讨论】:

    【解决方案3】:
    <?php
     session_start();
    require_once('../model/pdo.inc.php');
    // function for checking the username validation (not empty & Regex)
    function usernameValidation($username) // Username as parameter
    {
    if ( !empty($_POST['username']) )
    {
    $username = strtolower(htmlspecialchars($_POST['username'])); // username will be tranform to lowercase before regex matching
    
    if ( preg_match('#^[a-z0-9\.]{5,20}$#', $username) ) //  5 <= username lenght <= 20 in lowercase character to be valid
    {
        return true; // return true when the username is valid
    }
    else
    {
        echo "Invalid username, please re-try" ;
    }
    }
    else
    {
    echo "Enter your username";
    }
    }
    
    // function for checking the password validation (not empty & Regex)
    function passwordValidation($password) // Password as parameter
    {
    if ( !empty($_POST['password']) )
    {
    $password = htmlspecialchars($_POST['password']) ; // Protect the password
    
    if ( preg_match('#^[a-zA-Z0-9\.-_@$()]{6,10}$#', $password) ) // 6 <= password length <= 10 character to be valid
    {
        return true; // return true when password is valid
    }
    else 
    {
        echo "Invalid password, please re-try";   
    }
    }
     else
    {
    
    echo "Enter your password";    
    }
    }
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    if ( usernameValidation($username) == true AND   passwordValidation($password) == true )
    {
     // PDO Query (SELECT ...)
    }
    

    【讨论】:

    • 你好 Amitesh,你的脚本也出现了同样的问题
    【解决方案4】:

    将最后一个 if 条件更改为以下代码:

    if ( usernameValidation($_POST['username']) == true AND passwordValidation($_POST['password']) == true )
    {
    
    }
    

    在您的函数中仅使用变量 $username$password 而不是(!)$_POST['username']$_POST['password']

    【讨论】:

    • 嗨 Arlind,你能解释一下为什么我们不应该在函数中使用 $_['foo'] 吗?
    • 因为您通过参数将 $_POST['foo'] 传递给函数。所以 $_POST['username'] 的值被保存到 $username
    【解决方案5】:

    你定义了 $username 和 $password,它是 $_POST['username'] 和 $_POST['password']。你也可以制作没有参数的函数。通过进行这些更改,您的问题将得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多