Php 关于唯一用户名
我正在处理一个学生门户,其中我必须为学生创建一个唯一的登录用户名。我在csv文件中有他们的名字、姓氏和DOB。我尝试使用这三个参数来创建一个唯一的用户名。但DOB可能为空,因此可能会发生冲突 此外,一些学生作为一个课程的门户用户,例如ex:2012-13,将在2013-14年的csv表中提供详细信息,如果我们使用具有任何唯一时间戳或唯一id的相同格式生成用户名,则将有两个相同用户的用户名 我需要一个解决方案,我从中生成一个唯一的用户名与可用的详细信息,还可以检查每个学生在数据库中只输入一次,只有一个用户名 我希望我清楚的细节,如果不比请给你的意见,我会改进它 下面是我用来从csv数据生成用户名的代码 逻辑-USERNAME='first3个firstname字母'+'DOB'+'first3个firstname字母'Php 关于唯一用户名,php,mysql,csv,Php,Mysql,Csv,我正在处理一个学生门户,其中我必须为学生创建一个唯一的登录用户名。我在csv文件中有他们的名字、姓氏和DOB。我尝试使用这三个参数来创建一个唯一的用户名。但DOB可能为空,因此可能会发生冲突 此外,一些学生作为一个课程的门户用户,例如ex:2012-13,将在2013-14年的csv表中提供详细信息,如果我们使用具有任何唯一时间戳或唯一id的相同格式生成用户名,则将有两个相同用户的用户名 我需要一个解决方案,我从中生成一个唯一的用户名与可用的详细信息,还可以检查每个学生在数据库中只输入一次,只有
foreach($result作为$file\u数据){
//循环遍历数据并查找错误
//检查文件中的重复条目
如果(计数($已接受)>0){
foreach($accepted as$accepted\u用户){
如果($file_data['First name']==$accepted_user['First name'])&($file_data['Last name']==$accepted_user['Last name'])&($file_data['DOB']==$accepted_user['DOB']){
$rejected[]=array(“错误”=>$this->lang->line('duplicate\u entry\u in\u file'),“line\u number”=>$error\u count);
}
}
}
//从Firstname、Lastname和DOB生成用户名(如果为null,则添加自动生成的DOB)
如果($file_data['First name']=''''.|$$file_data['Last name']=''){//检查First_name Last_name和出生日期是否为空
$rejected[]=array(“错误”=>“名字,姓氏不可用”,“行号”=>$error\U count”);
}否则{
如果($file_data['DOB']=''){
$dob=日期('dmy',mktime(0,0,0,日期(“m”)、日期(“d”)+1,日期(“Y”)-10)//
出生日期=$dob['4']。$dob['5']。“-”$dob['2']。$dob['3']。“-”$dob['0']。$dob['1'];
$dob_username=$dob['0']。$dob['1']。“-”$dob['4']。$dob['3']。“-”$dob['4']。$dob['5'];
}否则{
$dob=$file_data['dob'];
如果(strlen($dob)==5){
$出生年份=$出生日期['3']。$出生日期['4'];
如果($出生年份<日期('y')){
$date_of_birth=“20”。$dob['3']。$dob['4']。“-”$dob['1']。$dob['2']。“-0”。$dob['0'];
$dob_username=“0-”$dob['0']。“-”$dob['1']。$dob['2']。“20”。$dob['3']。$dob['4'];
}elseif($出生年份>日期('y')){
$date_of_birth=“19”。$dob['3']。$dob['4']。“-”$dob['1']。$dob['2']。“-0”。$dob['0'];
$dob_username=“0-”$dob['0'].“-”$dob['2'].$dob['1'].“19”。$dob['3'].$dob['4'];
}
}elseif(斯特伦($dob)>5){
$出生年份=$出生日期['4']。$出生日期['5'];
如果($出生年份<日期('y')){
$date_of_birth=“20”。$dob['4']。$dob['5']。“-”$dob['2']。$dob['3']。“-”$dob['0']。$dob['1'];
$dob_username=$dob['0']。$dob['1']。“-”$dob['2']。$dob['3']。“-20”。$dob['4']。$dob['5'];
}否则{
$date_of_birth=“19”。$dob['4']。$dob['5']。“-”$dob['2']。$dob['3']。“-”$dob['0']。$dob['1'];
$dob_username=$dob['0']。$dob['1']。“-”$dob['2']。$dob['3']。“-19”。$dob['4']。$dob['5'];
}
}
}
$first_name=$file_data['first name'];
$last_name=$file_data['last name'];
$dateOfBirth=str_replace(“-”,“”,$dob_用户名);
if((strlen($last_name)<3)和&(strlen($first_name)<3)){
$username=$last_name.$dateOfBirth.$first_name;
}埃尔塞夫(斯特伦($last_name)<3){
//$username=$last_name.$dateOfBirth.substr($first_name,0,3);
$username=$last_name.$dateOfBirth.mb_substr($first_name,0,3,'UTF-8');
}埃尔塞夫(斯特伦($first_name)<3){
$username=mb_substr($last_name,0,3,'UTF-8')。$dateOfBirth.$first_name;
}否则{
$username=mb_substr($last_name,0,3,'UTF-8')。$dateOfBirth.mb_substr($first_name,0,3,'UTF-8');
}
//echo$username.“
”;
如果($user\u data=$this->user\u model->user\u exist(数组('username'=>$username))){
//如果用户已经存在,则采用用户id
foreach($user\u数据作为$user\u数据){
$user_id=$user_data['id'];
}
//如果用户已经在本季的项目中
如果($this->user\u model->user\u exist\u在\u项目中(数组('project\u id'=>$project\u name,'student'=>$username,'seasure'=>$seasure))){
//检查用户的名字和姓氏(如果存在)
如果($user\u data=$this->user\u model->user\u exist(数组($first\u name'=>$first\u name,'last\u name'=>$last\u name))){
//如果匹配
foreach($result as $file_data){
//loop throgh the data and find errors
//check the duplicate entry in the file
if(count($accepted)>0){
foreach($accepted as $accepted_user){
if(($file_data['First name'] == $accepted_user['First name']) && ($file_data['Last name'] == $accepted_user['Last name']) && ($file_data['DOB'] == $accepted_user['DOB'] )){
$rejected[]=array("error"=> $this->lang->line('duplicate_entry_in_file'),"line_number"=>$error_count);
}
}
}
// Generate username from Firstname , Lastname & DOB (if null than we add auto generated DOB)
if($file_data['First name']=='' || $file_data['Last name']==''){ // check if first_name last_name and date-of_birth is empty
$rejected[]=array("error"=> "First Name , Last Name not available","line_number"=>$error_count);
}else{
if($file_data['DOB']==''){
$dob=date('dmy',mktime(0,0,0,date("m"),date("d")+1,date("Y")-10)); //
$date_of_birth= $dob['4'].$dob['5']."-".$dob['2'].$dob['3']."-".$dob['0'].$dob['1'];
$dob_username= $dob['0'].$dob['1']."-".$dob['4'].$dob['3']."-".$dob['4'].$dob['5'];
}else{
$dob = $file_data['DOB'];
if(strlen($dob) == 5 ){
$year_of_birth=$dob['3'].$dob['4'];
if($year_of_birth < date('y')){
$date_of_birth= "20".$dob['3'].$dob['4']."-".$dob['1'].$dob['2']."-0".$dob['0'];
$dob_username= "0-".$dob['0']."-".$dob['1'].$dob['2']."20".$dob['3'].$dob['4'];
}elseif($year_of_birth > date('y')){
$date_of_birth= "19".$dob['3'].$dob['4']."-".$dob['1'].$dob['2']."-0".$dob['0'];
$dob_username= "0-".$dob['0']."-".$dob['2'].$dob['1']."19".$dob['3'].$dob['4'];
}
}elseif(strlen($dob) > 5 ){
$year_of_birth=$dob['4'].$dob['5'];
if($year_of_birth < date('y')){
$date_of_birth= "20".$dob['4'].$dob['5']."-".$dob['2'].$dob['3']."-".$dob['0'].$dob['1'];
$dob_username= $dob['0'].$dob['1']."-".$dob['2'].$dob['3']."-20".$dob['4'].$dob['5'];
}else{
$date_of_birth= "19".$dob['4'].$dob['5']."-".$dob['2'].$dob['3']."-".$dob['0'].$dob['1'];
$dob_username= $dob['0'].$dob['1']."-".$dob['2'].$dob['3']."-19".$dob['4'].$dob['5'];
}
}
}
$first_name=$file_data['First name'];
$last_name=$file_data['Last name'];
$dateOfBirth = str_replace("-", '', $dob_username);
if((strlen($last_name) < 3 ) && (strlen($first_name) < 3 ) ){
$username=$last_name.$dateOfBirth.$first_name;
}elseif(strlen($last_name) < 3 ){
// $username=$last_name.$dateOfBirth.substr($first_name, 0, 3);
$username=$last_name.$dateOfBirth. mb_substr($first_name, 0, 3,'UTF-8');
}elseif(strlen($first_name) < 3){
$username=mb_substr($last_name, 0, 3,'UTF-8').$dateOfBirth.$first_name;
}else{
$username=mb_substr($last_name, 0, 3 ,'UTF-8').$dateOfBirth.mb_substr($first_name, 0, 3,'UTF-8');
}
//echo $username."<br/>";
if($user_datas = $this->user_model->user_exist(array('username'=>$username))){
//if user already exist then take the user_id
foreach($user_datas as $user_data){
$user_id=$user_data['id'];
}
//if the user is already in the project for this season
if($this->user_model->user_exist_in_project(array('project_id'=>$project_name,'student'=>$username,'season'=>$season))){
//check users first name and last name if exist
if($user_datas = $this->user_model->user_exist(array('first_name'=>$first_name,'last_name'=>$last_name))){
//if match found then reject this data
$rejected[]=array("error"=> $this->lang->line('student_already_in_project_in_season'),"line_number"=>$error_count);
}else{
$accepted[]=$file_data;
}
}
}else{
$accepted[]=$file_data;
}
}
$error_count++;
}
$fname = "John";
$lname = "Doe";
$bday = "1980-03-01";
$unique_user_string = strtolower($fname . $lname . $bday); // not actually unique if 2 people have same name and birthday
$username = ucfirst(substr($fname, 0, 1)) . ucfirst($lname) . crc32($unique_user_string);
$crc = substr($username, -10, 10);
$uniqueid2 = md5(uniqid());
$uniqueid = substr($uniqueid2, 0, 8);