PHP帮助,无法识别不同的用户

时间:2014-04-08 10:30:30

标签: php

我对PHP知之甚少,而且我被指派去尝试解决一些在网站上不起作用的事情。该网站主要处理两个不同的用户,一个可以发布文章的交易者和一个可以发布博客的博主。当用户注册成为交易者并尝试提交文章时,该页面只会重定向到base_url.user/blogger/而不是base_url.user/trader/。我认为这是因为它并没有将用户识别为交易者。你能看一下代码吗?如果你有任何关于它为什么不起作用的想法我会接受你的想法

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
     * Index Page for this controller.
     *
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed 
     *
     * 
     * File Controller; location: application/controllers/admin.php
     * Author : Pradeep
     * Date  :  Sunday Feb 5th, 2012
**/
class Register extends CI_Controller {


    private $user;
    private $logData;


    function _Register()
    {
        parent::controller();
        $this->config->load('constants.php');


    }
    public function blogger()
    {
        //-------------------------SENDING ERROR MESSAGE FOR LOGIN-------------------
        $data['captchaError'] = '';
        $data['captcha'] = $this->getCaptcha();
        $data['pageTitle']='Registration | Blogger';
        $this->load->view('register-blogger',$data);
    }
    public function trader()
    {
        $data['captchaError'] = '';
        $data['captcha'] = $this->getCaptcha();
        $data['pageTitle']='Registration | Trader';
        $this->load->view('register-trader',$data);
    }
    public function activate()
    {

        $data['pageTitle']='Registration | Succesfully registered';
        $this->load->view('register-success',$data);
    }


    public function process()
    {
        $date = date('Y-m-d');
        $userid = uniqid();
        $captchaError = '';
        if($this->input->post('Submit'))
        {
//---------------------------------FORM VALIDATION STARTS HERE---------------------------------
            $this->form_validation->set_error_delimiters('', '');
            $this->form_validation->set_rules('fname', 'Full name','required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[tbl_user.email]');
            $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]|matches[cpassword]');
            $this->form_validation->set_rules('cpassword', 'Password confirmation', 'required');
            $this->form_validation->set_rules('mycheck[]', 'Buyer or Supplier','required');
            $this->form_validation->set_rules('material[]', 'materials','required');
            $this->form_validation->set_rules('company', 'Company name', 'required');
            $this->form_validation->set_rules('cname', 'Contact name','required');
            $this->form_validation->set_rules('cemail', 'Contact email', 'required|valid_email');
            $this->form_validation->set_rules('nation', 'Country', 'required');
            $this->form_validation->set_rules('city', 'City','required');
            $this->form_validation->set_rules('fax');
            $this->form_validation->set_rules('mobile');
            $this->form_validation->set_rules('phone');
            $this->form_validation->set_rules('website');
            $this->form_validation->set_rules('address');
            $this->form_validation->set_rules('zip');
            $this->form_validation->set_rules('content', 'Tell something about urself', 'required');
            $this->form_validation->set_rules('captchaText', 'captcha text', 'required');
//-----------------------------------FORM VALIDATION ENDS HERE--------------------------------------


//------------------------------------CAPTCHA CHECK------------------------------------------
            if($this->input->post('captchaText'))
            {
                $expiration = time()-7200; // Two hour limit
                $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

            // Then see if a captcha exists:
                $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
                $binds = array($_POST['captchaText'], $this->input->ip_address(), $expiration);
                $query = $this->db->query($sql, $binds);
                $row = $query->row();

                if ($row->count == 0)
                {
                $captchaError =  "You must submit the word that appears in the image";
                }
            }
//--------------------------------------CAPTCHA CHECK ENDS HERE----------------------------

//----------------------------------FORM VALIDATION RETURN ERRORS---------------------------
            if ($this->form_validation->run() == FALSE || $captchaError!='')
            {
            $data['captcha'] = $this->getCaptcha();
            $data['captchaError'] = $captchaError;
            $data['pageTitle']='Registration | Error';
            $this->load->view('register-trader',$data);
            }
//-----------------------------------------------END---------------------------------------

//---------------------------------------INSERT DATA INTO DATABASE-----------------------
            else
            {
            if($this->input->post('material'))
                {
                $material = '';
                foreach($this->input->post('material') as $value)
                {
                $material.= $value.',';
                }
            $material = rtrim($material,',');
            }
            $mycheck = $this->input->post('mycheck');
            $mycheckOne = '';
            $mycheckTwo = '';
            if(!empty($mycheck[0])){$mycheckOne = $mycheck[0];}
            if(!empty($mycheck[1])){$mycheckTwo = $mycheck[1];}
            $config['file_name'] = uniqid(); 
            $config['upload_path'] = UP_PATH;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload('userfile1'))
            {               
                $error = $this->upload->display_errors();
                $data = array(
                  'supplier'=>$mycheckOne,
                  'buyer'=>$mycheckTwo,
                  'title'=>$this->input->post('company'),
                  'cname'=>$this->input->post('cname'),
                  'material'=>$material,
                  'email'=>$this->input->post('email'),
                  'phone'=>$this->input->post('phone'),
                  'fax'=>$this->input->post('name'),
                  'mobile'=>$this->input->post('mobile'),
                  'web'=>$this->input->post('website'),
                  'country'=>$this->input->post('nation'),
                  'city'=>$this->input->post('city'),
                  'address'=>$this->input->post('address'),
                  'zip'=>$this->input->post('zip'),
                  'content'=>$this->input->post('content'),
                  'date'=>$date,
                  'userid'=>$userid,
                  'status'=>0
                );

            }   
            else
            {
                $data = array('upload_data' => $this->upload->data()); 
                $filepath = $data['upload_data']['file_name'];
                $config['image_library'] = 'gd2';
                $config['source_image'] = UP_PATH.$filepath;
                $config['new_image'] = UP_PATH.'thumbs/';
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '';
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 75;
                $config['height'] = 50;
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();

                $data = array(
                  'supplier'=>$mycheckOne,
                  'buyer'=>$mycheckTwo,
                  'title'=>$this->input->post('company'),
                  'cname'=>$this->input->post('cname'),
                  'material'=>$material,
                  'email'=>$this->input->post('email'),
                  'phone'=>$this->input->post('phone'),
                  'fax'=>$this->input->post('fax'),
                  'mobile'=>$this->input->post('mobile'),
                  'web'=>$this->input->post('website'),
                  'country'=>$this->input->post('nation'),
                  'city'=>$this->input->post('city'),
                  'address'=>$this->input->post('address'),
                  'zip'=>$this->input->post('zip'),
                  'content'=>$this->input->post('content'),
                  'image'=>$filepath,
                  'date'=>$date,
                  'userid'=>$userid,
                  'status'=>0
                  );

            }   
            $this->db->insert(TBL_CLA,$data);
            $log_type = 'trader';
            $password = do_hash($this->input->post('password'));
            $dataOne = array(
                  'password'=>$this->security->xss_clean($password),
                  'fname'=>$this->security->xss_clean($this->input->post('fname')),
                  'email'=>$this->security->xss_clean($this->input->post('email')),
                  'log_type'=>$log_type,
                  'userid'=>$userid,
                  'status'=>0,
                  'date'=>$date,
                  'active'=>1
                  );
            $this->db->insert(TBL_USE,$dataOne);

            $this->session->set_userdata('fname', $this->input->post('fname'));
            redirect(base_url().'register/activate');
            }
            }
            if($this->input->post('Login'))
            {
//---------------------------------FORM VALIDATION STARTS HERE---------------------------------
            $this->form_validation->set_error_delimiters('', '');
            $this->form_validation->set_rules('fname', 'Full name','required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[tbl_user.email]');
            $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]|matches[cpassword]');
            $this->form_validation->set_rules('cpassword', 'Password confirmation', 'required');
            $this->form_validation->set_rules('captchaText', 'captcha text', 'required');
//-----------------------------------FORM VALIDATION ENDS HERE--------------------------------------


//------------------------------------CAPTCHA CHECK------------------------------------------
            if($this->input->post('captchaText'))
            {
                $expiration = time()-7200; // Two hour limit
                $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

            // Then see if a captcha exists:
                $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
                $binds = array($_POST['captchaText'], $this->input->ip_address(), $expiration);
                $query = $this->db->query($sql, $binds);
                $row = $query->row();

                if ($row->count == 0)
                {
                $captchaError =  "You must submit the word that appears in the image";
                }
            }
//--------------------------------------CAPTCHA CHECK ENDS HERE----------------------------

//----------------------------------FORM VALIDATION RETURN ERRORS---------------------------
            if ($this->form_validation->run() == FALSE || $captchaError!='')
            {
            $data['captcha'] = $this->getCaptcha();
            $data['captchaError'] = $captchaError;
            $data['pageTitle']='Registration | Error';
            $this->load->view('register-blogger',$data);
            }
//-----------------------------------------------END---------------------------------------

//---------------------------------------INSERT DATA INTO DATABASE-----------------------
            else
            {
            $date = date('Y-m-d');
            $log_type = 'blogger';
            $password = do_hash($this->input->post('password'));
            $dataOne = array(
                  'password'=>$this->security->xss_clean($password),
                  'fname'=>$this->security->xss_clean($this->input->post('fname')),
                  'email'=>$this->security->xss_clean($this->input->post('email')),
                  'log_type'=>$log_type,
                  'userid'=>$userid,
                  'status'=>0,
                  'date'=>$date,
                  'active'=>0
                  );
            $this->db->insert(TBL_USE,$dataOne);
            $data['link'] = 'http://www.arabrecycling.org/activate/created/'.$userid;
            $data['name'] = $this->input->post('fname');
            $message = $this->load->view('includes/activate',$data, TRUE);
            $subject = 'Account Activation';
            $fromTest = 'The Arab Recycling Initiative';
            $this->userRegEmail('info@arabrecycling.org',$this->input->post('email'),$message,$subject,$fromTest);
            $this->session->set_userdata('fname', $this->input->post('fname'));
            redirect(base_url().'register/activate');
            }
            }


    }
//-------------------------------------------------------CAPTCHA CREATION STARTS HERE------------------------
    public function getCaptcha(){

            $this->load->library('common');
            $this->common = new common();

            $this->load->helper('captcha');
            $vals = array(
            'word' => $this->common->GetRandomCaptchaText(8),
            'img_path' => './captcha/',
            'img_url' => base_url().'captcha/',
            'font_path' => base_url().'system/fonts/Candice.ttf',
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200
            );

            $cap = create_captcha($vals);

            $data = array(
            'captcha_time' => $cap['time'],
            'ip_address' => $this->input->ip_address(),
            'word' => $cap['word']
            );

            $query = $this->db->insert_string('captcha', $data);
            $this->db->query($query);

            return $cap['image'];
    }
//--------------------------------------------------------CAPTCHA CREATION ENDS HERE------------------------------------------------
//--------------------------------------------------------CONFIGURING EMAIL------------------------------------------------
        public function userRegEmail($from,$to,$message,$subject,$fromTest){
             $email_config['protocol'] = 'mail';
             $email_config['mailtype'] = 'html';
             $this->email->initialize($email_config);

             $this->email->from($from, $fromTest);
             $this->email->to($to);         
             $this->email->subject($subject);
             $this->email->message($message); 
             $this->email->send();
    }
//--------------------------------------------------------EMAIL CONFIGURATION ENDS HERE------------------------------------------------

2 个答案:

答案 0 :(得分:0)

您应该确定用户是交易员还是博主,然后相应地设置适当的会话变量。根据设置的会话变量来检查哪个登录的简单if语句应该可以解决?如果我正确理解你的问题。

我建议(可能更容易)是

<强> 1。向数据库中的用户表添加一个属性,即Account_Type枚举('trader','blogger')not null 。

<强> 2。注册时,在表单中添加单选按钮,以选择他们是交易者还是博主 并且显然已将此数据发送回您的数据库

第3。当他们登录时(我假设所有用户都有一个唯一的用户名)执行类似

的操作

- 将会话变量设置为0

  $_SESSION['trader'] = 0;
  $_SESSION['blogger'] = 0;

- 在数据库中查找用户名,并查找该用户名是否为交易者或博主。

//A Query to find the Account_type that goes with the input account 
$accquery = "SELECT Account_type FROM Account WHERE Account_Name =$_POST['account']";

//Running the query
$result = mysqli_query($connect,$accquery);


//Using an array to fetch the type and put it into a variables place
while($row = mysqli_fetch_array($connect,$result))
{
$acctype = $row['Account_Type']; 
}



else {
    die("Query failed");
}

然后执行类似

的操作
if($acctype == "trader") {
  $_SESSION['trader'] = 1;
elseif($acctype == "Blogger") {
  $_SESSION['blogger'] =1;
else {
  //error message
}

然后当你需要检查它们是什么时,你可以做类似的事情

 if((isset($_SESSION['trader'])) && ($_SESSION['trader'] == "1")){
   //specific code for trader
   }

您可以将其应用于博主。

我还建议你研究消毒你的输入,这很简单但非常重要。

另请注意,通过stackoverflow彻底搜索您的问题的答案,因为您很可能会找到一些东西。

- 希望这有帮助,请注意它只是粗略的,所以你必须解决它,但你应该掌握一些PHP,所以应该这样做。

答案 1 :(得分:0)

使用SESSIONS:

在您设置的页面顶部:

<?php 
  //Check if session has not been started.
  if (session_status() == PHP_SESSION_NONE)
  {
    session_start();
  }
?>

在定义交易者或博客时的代码中:

$_SESSION['USER'] = "trader";

$_SESSION['USER'] = "blogger";

然后你可以执行你想要的代码:

//If it has not been set, do trader by default.
if (!ISSET($_SESSION['USER'])
{
    $_SESSION['USER'] = "trader";
}

if ($_SESSION['USER'] == "blogger")
{
   //Execute code for blogger
}
else if($_SESSION['USER'] == "trader")
{
   //Execute code for trader
}

只要您的“页面/会话”处于打开状态,会话就会存储在您的浏览器中。那些“变量”是用户特定的。

希望这有助于:)

相关问题