更改Codeigniter网站的默认主页

时间:2015-04-20 15:39:57

标签: php codeigniter

请帮我看看如何更改codeigniter网站的默认主页..当我去主页(http://postcodepals.co.uk)时,它会重定向到(http://postcodepals.co.uk/Login) ..

这是login.php的控制器

 class Login extends MY_Controller {

    function __construct()
    {
        parent::__construct();  
        $this->data['title'] = "Login";
        $this->load->model('Login_model');
//      $this->load->database();
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/Login
     *  - or -  
     *      http://example.com/index.php/Login/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        if(isset($this->session->userdata['id'])){
            header('Location:'.base_url().'index.php/Home');
            return;
        }

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'callback_auth');

        if ($this->form_validation->run() == FALSE)
        {
            //set session to change the top banner
            $this->load->model('Master_config_model');
            $data=array();
            $data['master_config']=$this->Master_config_model->get_main_data();
            $this->session->set_userdata($data);

            //show login page
            $this->view('login');
        }
        else
        {
            header('Location:'.base_url().'index.php/Home');
        }       
    }

    function auth($str)
    {
        $ret=$this->Login_model->GetLogin($this->input->post('email'), $this->input->post('pass'));
        if ($ret){
            return TRUE;
        }else{
            $this->form_validation->set_message(__FUNCTION__,'Email/Password is wrong');
            return FALSE;
        }
    }

    public function logout(){
        $master_user=$this->session->userdata('master_user');
//      pr($master_user,'$master_user');exit;
        if ($master_user){
            $this->session->unset_userdata('master_user');
            $this->session->set_userdata($this->Login_model->get_user_session($master_user));
        }else{
//          $this->session->unset_userdata('id');
            $this->session->sess_destroy();
        }
        $this->index();
    }

    function email_exist($str)
    {
        $ret=$this->Login_model->get_user($this->input->post('email'));
        if ($ret){
            return TRUE;
        }else{
            $this->form_validation->set_message(__FUNCTION__,'Email is wrong');
            return FALSE;
        }
    }

    public function reset(){
        $this->data['title'] = "Reset Password";
        $this->data['html_hint'] = "";
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation','email'));

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('email', 'Email', 'required|callback_email_exist');

        if ($this->form_validation->run() == FALSE)
        {
            $this->view('reset');
        }
        else
        {
            //create new password
            $new_pass=generate_password();
            //update database
            $ret=$this->Login_model->set_pass($this->input->post('email'), $new_pass);          
            //send email
//          $this->email->from('john@shuitest.dyndns.biz', 'postcode checker');
            $this->email->to($this->input->post('email'));

            $this->email->subject('Your new password for '.base_url());
            $this->email->message('Your new password is '.$new_pass);           
            $this->email->send();

//          $this->form_validation->set_error_delimiters('<div class="hint">', '</div>');
//          $this->form_validation->set_message('email','Your new password is sent');
//          pr($this->form_validation,'$this->form_validation');exit;
            $this->data['html_hint'] = "Your new password is sent";
            $this->view('reset');
        }       
    }

    function change_pass(){
        $this->data['title'] = "Reset Password";
        $this->data['html_hint'] = "";

        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation','email'));

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        $this->form_validation->set_rules('pass2', 'Confirm Password', 'required|callback_is_pass_same');

        if ($this->form_validation->run() == FALSE)
        {
            $this->view('change_pass');
        }
        else
        {
            //update database
            $ret=$this->Login_model->set_pass($this->session->userdata('email'), $this->input->post('pass'));

            $this->data['html_hint'] = "Your password is changed";
            $this->view('change_pass');
        }       
    }

    public function is_pass_same($str){
        if ($this->input->post('pass')==$this->input->post('pass2')){
            return TRUE;
        }else{
            $this->form_validation->set_message(__FUNCTION__,'Confirm password is different from the password');
            return FALSE;
        }
    }

    public function goclick($log_id){
        $url=$this->Login_model->get_click($log_id);
        $url=prep_url($url);
        redirect($url);
    }

    public function checker($user_id){
        $postcode=$this->input->get('postcode');
        $outward=$this->input->get('outward');
        $ret=$this->Login_model->check_postcode($user_id, $postcode, $outward);
        $ret=preg_replace('/\s+/',' ',$ret);
        $ret=str_replace("'",'\\\'',$ret);
        echo "pchecker_callback('$ret');";
    }

    public function show($user_id, $password){
        $this->load->model('Master_config_model');
        $master_config=$this->Master_config_model->get_main_data();
//      pr($master_config,'$master_config');
        if (isset($master_config['seo_link']) && $master_config['seo_link']){
            $seo_link="<a href='{$master_config['seo_link']}' style='text-decoration:none'>{$master_config['seo_title']}</a>";
        }else{
            $seo_link='';
        }
        $design=$this->Login_model->get_checker($user_id);
        if (!$design){
            $design=array(
                'size'=>'160 x 155',
                'color_title'=>'',
                'color_bg'=>'',
                'color_text'=>'',
                'color_border'=>'',
            );
        }

        $user=$this->Login_model->get_user_md5($user_id, $password);
//      pr($user,'$user');
        if (!$user){
            //show image
            $img_file='img'.str_replace(' x ', '_', $design['size']).'.png';
            $img_path=base_url("upload/$img_file");
            $inactive_link=$master_config['inactive_link'];
            $inactive_link=prep_url($inactive_link);
            ob_start();
            include(FCPATH.'js/pchecker_close.htm');
            $out=ob_get_contents();
            ob_end_clean();
            $data=preg_replace('|(?si)[\r]?\n|',' ',$out);
            $data=str_replace("'",'\\\'',$data);
            $data=array($data);
            include(FCPATH.'js/show.js');
            return;
        }

        {
//          pr($design);
            switch($design['size']){
            case '123 x 136':
                $css_title="font-size: 13px; padding: 10px 0; ";
//              $css_text="font-size: 11px; padding: 0 0 10px 0;";
                $css_text="font-size: 12px; padding: 0 0 10px 0;";
                break;
            case '160 x 155':
                $css_title="font-size: 16px; padding: 10px 0; ";
//              $css_text="font-size: 15px; padding: 0 0 10px 0;";              
                $css_text="font-size: 16px; padding: 0 0 10px 0;";              
                break;
            case '300 x 66':
                $css_title="font-size: 12px; padding: 4px 0; ";
//              $css_text="font-size: 10px; padding: 0 0 4px 0;";
                $css_text="font-size: 11px; padding: 0 0 4px 0;";
                break;
            }
            list($pchecker_w, $pchecker_h)=explode(' x ', $design['size']);
//          $css_message=$css_text."height:".($pchecker_h-47)."px;width:".($pchecker_w-20)."px;margin:10px auto 0 auto;background-color:"
//              .($design['color_bg']?$design['color_bg']:'white').";color:"
//              .($design['color_bg']?$design['color_text']:'black');
            $css_message=$css_text."height:".($pchecker_h-47)."px;width:".($pchecker_w-20)."px;margin:10px auto 0 auto;background-color:transparent;color:"
                .($design['color_text']?$design['color_text']:'black');

            //visibility
//          $pchecker_border=($design['color_border'])?"border: 2px solid {$design['color_border']};":'';
            $pchecker_border='';
            $color_title=($design['color_title'])?"color: {$design['color_title']};":'visibility: hidden;';
            $color_text=($design['color_text'])?"color: {$design['color_text']};":'visibility: hidden;';
//          $color_bg=($design['color_bg'])?"background-color: {$design['color_bg']};":'';
            $color_bg='';
            $base_upload=base_url("upload");
            if (file_exists(FCPATH.'upload/'.$user_id.'.gif')){
                $rand=mt_rand();
                $img_bg="background-image: url(&quot;$base_upload/$user_id.gif?$rand&quot;);";
            }else{
                $img_bg='background-image:none;';
            }
        }
        $base_js=base_url("js");
        //http://shuitest.dyndns.biz/richierich86/bouncyparty/post_code.php
        $checker_url=site_url('Login/checker/'.$user_id);
//      pr($checker_url,'$checker_url');

//      $data=file(FCPATH.'js/pchecker.htm',FILE_IGNORE_NEW_LINES);
        ob_start();
        include(FCPATH.'js/pchecker.htm');
        $out=ob_get_contents();
        ob_end_clean();
//      $data=preg_split('|(?si)[\r]?\n|',$out);
        $data=preg_replace('|(?si)[\r]?\n|',' ',$out);
        $data=str_replace("'",'\\\'',$data);
        $data=array($data);
//      pr($data,'$data');

        include(FCPATH.'js/show.js');
//      echo "<xmp>";print_r(get_declared_classes());echo "</xmp>";
//echo "<xmp>";print_r(get_defined_constants());echo "</xmp>";
//echo "<xmp>";$c=get_defined_constants(true);print_r($c['user']);echo "</xmp>";
//      echo 'to be continued';
    }   
}

/* End of file Login.php */
/* Location: ./application/controllers/Login.php */

以下是登录的视图

    <?php $this->load->view("header"); ?>
<?php 
//echo base_url();
//pr($this); 
?>
<table width="100%" height="100%" cellspacing="0" cellpadding="5" border="0">
<tbody><tr><td valign="middle">

<table width="400" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody><tr><td>
<fieldset>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
    <td valign="top" class="usual" colspan="3">

<?php echo form_open('Login'); ?>
<table align="center">
<tbody>
<tr>
    <td align=center colspan=2>
<?php echo validation_errors(); ?>
        </td>
</tr>
<tr>
    <td>&nbsp;</td>
</tr>

<tr>
    <td align="right">
    Email&nbsp;
    </td>
    <td>
        <input type="text" maxlength="32" size="24" value="<?php echo set_value('email'); ?>" name="email" class="field">
    </td>
</tr>
<tr>
    <td align="right">
    Password&nbsp;
    </td>
    <td>
        <input type="password" maxlength="32" size="24" value="<?php echo set_value('pass'); ?>" name="pass" class="field">
    </td>
</tr>

<tr>
    <td>
    </td>
    <td>
        <input type="submit" value=" Login " class="submit">
        <input type="button" value="Reset Password" class="submit" onclick="location.href='<?=base_url()?>index.php/Login/reset'">
    </td>
</tr>
<tr>
    <td>&nbsp;</td>
</tr>
</tbody></table>
</form>
    </td>
</tr>
</tbody></table></fieldset>



    </td>
</tr>
</tbody></table>

</td></tr></tbody></table>
<?php $this->load->view("footer"); ?>

我需要的是拥有mainhome.php,当我访问http://postcodepals.co.uk时,主要内容将会出现,而不是重定向到http://postcodepals.co.uk/Login

请帮助..谢谢

1 个答案:

答案 0 :(得分:4)

看起来你的路线有问题。尝试将mainhome.php设置为路线中的默认控制器。

转到您的项目 - &gt;应用程序 - &gt; config-&gt; routes.php

并更改

$route['default_controller'] = 'login';

$route['default_controller'] = 'mainhome';