简体   繁体   English

如果与会话匹配,PHP 保存数组值

[英]PHP save array value if match to Session

I have an Input submit form and i want if an user enters an number thats match with my array value , the Card Brand saves to PHP Session on next site.我有一个输入提交表单,我希望如果用户输入一个与我的array value匹配的数字,卡片品牌将保存到下一个站点的 PHP 会话中。

<?php    
    $submitbutton= $_POST['btnLogin'];    
    $number= $_POST['Kreditkartennummer'];

    function validatecard($number)
    {
        global $type;

        $cardtype = array(
            "visa"       => "/^4[0-9]{12}(?:[0-9]{3})?$/",
            "mastercard" => "/^5[1-5][0-9]{14}$/",
            "amex"       => "/^3[47][0-9]{13}$/",
            "discover"   => "/^6(?:011|5[0-9]{2})[0-9]{12}$/",
        );

        if (preg_match($cardtype['visa'],$number))
        {
            $type= "visa";
            return 'visa';          
        }
        else if (preg_match($cardtype['mastercard'],$number))
        {
            $type= "mastercard";
            return 'mastercard';
        }
        else if (preg_match($cardtype['amex'],$number))
        {
            $type= "amex";
            return 'amex';          
        }
        else if (preg_match($cardtype['discover'],$number))
        {
            $type= "discover";
            return 'discover';
        }
        else
        {
            return false;
        } 
    }    
    validatecard($number);    
?>

The Question now, works it with my Code?现在的问题,是否适用于我的代码? or needs an "If Submit" ?还是需要"If Submit" The other question how can i echo the return and save it to my php Session ?另一个问题我如何回显return并将其保存到我的 php Session

To save something to your session variable all you have to do is declare it so.要将某些内容保存到您的会话变量中,您所要做的就是声明它。

$_SESSION['card_type'] = $_POST['card_type'];

for example let's say this is your form.例如,假设这是您的表格。 This is only an example.这只是一个例子。

<form type="POST">
<input type="text" name="card_type">
<input type="submit" value="submit my form">
</form>

in this form, you have an input with the name card_type, when this is submitted you can get the value from that input like so.在此表单中,您有一个名为 card_type 的输入,当提交时,您可以像这样从该输入中获取值。

    if(isset($_POST['card_type'])) {
    $_SESSION['card_type'] = $_POST['card_type']; //you are taking the post and making a session variable.
}

I know this only explains the last part of your questions, I just did not understand the first part.我知道这只能解释你问题的最后一部分,我只是没有理解第一部分。

edit..I also wanted to point out that you do not just want to accept the user input without some type of validation.编辑..我还想指出,您不只是想在没有某种类型验证的情况下接受用户输入。 Put you validation code after you check if there has been a post.在您检查是否有帖子后放置验证码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM