无论我做什么,PHP都会将变量设置为数组?

时间:2011-05-28 20:23:29

标签: php mysql scripting

这是我遇到的最奇怪的错误,我在这里结束了。它只是从脚本开始,甚至知道我改变的唯一事情是在名字和姓氏变量上运行大写功能。

问题在于“$ lname”变量。我已经在调试中得到了脚本,并且我在每个进程中都回显了“$ lname”,并且只有在我将它设置为_SESSION变量后才会变为数组。我所做的一切似乎都没有用。每次用户返回表单时,它都会取消设置所有会话变量,但我甚至尝试更改“$ _SESSION ['reglname']的名称;我仍然会发生同样的事情。

我在这里结束了,如果你仔细阅读,你可以看到我已经尝试了所有的东西来强制它作为一个字符串。我想我可以编辑后续脚本来说明它是一个数组,但我宁愿弄清楚这里发生了什么,所以我可以在将来避免它!请帮助!

//Start session
session_start();

unset($_SESSION['ERRMSG_ARR']);

//Include database connection details
require_once("$DOCUMENT_ROOT/../SQLlogin.php");

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Sanitize the POST values
$fname = clean($_POST['FirstName']);
$lname = clean($_POST['LastName']);
$email = clean($_POST['Email']);
$cemail = clean($_POST['ConfirmEmail']);
$bday = clean($_POST['BirthDay']);
$bmonth = clean($_POST['BirthMonth']);
$byear = clean($_POST['BirthYear']);

echo $lname;

//Capitalize first and last name
$fname = ucwords($fname);
$lname = ucwords($lname);

//Collate and format birthdate
if ($bmonth < 10){
    $bdate = clean("0".$bmonth."/".$bday."/".$byear);
}else{
    $bdate = clean($bmonth."/".$bday."/".$byear);
}

//Echo out vars to check
echo $fname."<br />".$lname;

//Server-side validations
if($fname == '' || strlen($fname) < 2 || strlen($fname) > 24 || strpbrk($fname, " ")) {
    $errmsg_arr[] = '*First name must be greater than 2 characters, less than 24, and contain no spaces!';
    $errflag = true;
}
if($lname == '' || strlen($lname) < 2 || strlen($lname) > 24 || strpbrk($lname, " ")) {
    $errmsg_arr[] = '*Last name must be greater than 2 characters, less than 24, and contain no spaces!';
    $errflag = true;
}
if(!ereg("[a-zA-Z]+", $fname, $lname)){
    $errmsg_arr[] = '*First and last name can contain only letters!';
    $errflag = true;
}
if ($email == '' || strlen($email) < 2 || strlen($email) > 32) {
    $errmsg_arr[] = '*Email address must be greater than 3 and less than 32 characters!';
    $errflag = true;
}
if ($cemail == '') {
    $errmsg_arr[] = '*Email address must be greater than 3 and less than 32 characters!';
    $errflag = true;
}
if ($email != $cemail) {
    $errmsg_arr[] = '*Email addresses do not match!';
    $errflag = true;
}
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
    $errmsg_arr[] = '*The email address entered appears to be invalid. Please enter a valid email address';
    $errflag = true;
}
if($bday == '') {
    $errmsg_arr[] = '*Birthday field left blank!';
    $errflag = true;
}
if($bmonth == '') {
    $errmsg_arr[] = '*Birth month field left blank';
    $errflag = true;
}
if($byear == '') {
    $errmsg_arr[] = '*Birth year field left blank';
    $errflag = true;
}
$year = date(Y);
if(($year - $byear) < 18){
    $errmsg_arr[] = '*You must be 18 years or older to register as a Hoppr client!';
    $errflag = true;
}
$allowed_age = 18;
$bdatecheck = strtotime($byear.'-'.$bmonth.'-'.$bday);
$age = (time()-$bdatecheck)/31536000;
if($age >= $allowed_age) {

}else{
    $errmsg_arr[] = '*You must be 18 years or older to register as a Hoppr client!';
    $errflag = true;
}

//If there are input validations, redirect back to the registration form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: http://www.thehoppr.com/clients/registration.php");
    exit();
}

echo $_SESSION['reglname'];

//Set registration session vars
$_SESSION['regstep'] = (int) 0;
$_SESSION['regfname'] = (string) $fname;
(string) $_SESSION['reglname'] = (string) $lname;
$_SESSION['regemail'] = (string) $email;
$_SESSION['regbdate'] = (string) $bdate;

echo $_SESSION['reglname'];

//Perform the email check query
$checkqry = "SELECT * FROM clients WHERE email='".mysql_real_escape_string($email)."'";
$checkresult = mysql_query($checkqry);
$checkAry = mysql_fetch_array($checkresult, MYSQL_ASSOC);
if (empty($checkAry)){
    //No email found, send user to step 2
    $_SESSION['regstep'] = (int) 2;
}else{
    //Dupl email found, check for Hoppr registration
    $isReg = $checkAry['isReg'];
    if ($isReg == 0) {
        global $checkAry;
        //Email found but not registered. Send user to Facebook merge page
        $_SESSION['regstep'] = (int) 1;
        $_SESSION['regfbname'] = $checkAry['firstname']." ".$checkAry['lastname'];
        $_SESSION['regfbpic'] = $checkAry['imgURL'];
        $_SESSION['regfbid'] = $checkAry['facebookID'];
        $_SESSION['regfbbday'] = $checkAry['birthday'];
    }else{
        //Email found and is registered. Ask user for a different email
    }
}

1 个答案:

答案 0 :(得分:7)

这是你的问题:

if(!ereg("[a-zA-Z]+", $fname, $lname)){

我想你想要:

if(!ereg("[a-zA-Z]+", $fname.$lname)){

Ereg将在参数3中存储匹配。