0

I have run this script many times on local server and there was no problem. however when I uploaded them on web server I faced undefined errors. I get confused.

What is the problem??

Database tables already defined.

PHP version on local: 5.3.5, on web server: 5.3.26

I get these errors:

            A PHP Error was encountered

            Severity: Notice
            Message:  Undefined index: username
            Filename: models/user.php
            Line Number: 7

            A PHP Error was encountered

            Severity: Notice
            Message:  Undefined index: password
            Filename: models/user.php
            Line Number: 8

            A PHP Error was encountered

            Severity: Warning
            Message:  Cannot modify header information - headers already sent by (output started at /home/caspian/domains/caspiansang.ir/public_html/system/core/Exceptions.php:185)
            Filename: views/login.php
            <p>Line Number: 1

Controller/verifylogin.php:

            <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

            class VerifyLogin extends CI_Controller {

             function __construct()
             {
               parent::__construct();
               $this->load->model('user','',TRUE);
             }

             function index()
             {

                    $result = $this->user->validate();
                    if(! $result){
                     $data['msg'] = 'اطلاعات وارد شده اشتباه است!';
                    $this->load->view('login',$data);
                    }else{
                        redirect('admin');
                    }        
                }


            }

Models/user.php

            <?php
            Class User extends CI_Model
            {

             public function validate(){

                    $username = $_POST['username'];
                    $password = $_POST['password'];


                    // Prep the query
                    $this->db->where('username', $username);
                    $this->db->where('password', md5($password));

                    // Run the query
                    $query = $this->db->get('admin');
                    // Let's check if there are any results
                    if($query->num_rows() == 1)
                    {
                        // If there is a user, then create session data
                        $row = $query->row();
                        $data = array(
                                'userid' => $row->userid,
                                'fname' => $row->fname,
                                'lname' => $row->lname,
                                'username' => $row->username,
                                'validated' => true
                                );
                        $this->session->set_userdata($data);
                        return true;
                    }

                    return false;
                }


            }

Views/login.php:

       <?php echo form_open('verifylogin'); ?>
           <br>
                    <div style="font-color:black; padding-bottom:10px;"> <?php if(!   empty($msg)) echo $msg;?></div>        

         < label for="username">نام کاربری</label>
         <input type="text" size="20" id="username" name="username"/>
         <br/>
         <label for="password">رمز عبور</label>
         <input type="password" size="20" id="passowrd" name="password"/>
         <br/>
Ahmad Ali
  • 1
  • 4
  • Your $_POST superglobal does not contain indexes named 'username' and 'password'. This usually happens because you did not submit a form using the 'post' method, or because your form does not contain fields name 'username' and 'password.' Based on the HTML you provided, I suspect that you are not submitting the form via POST. – George Cummins Jul 11 '13 at 15:23
  • The error on line 1 says it all... the other warnings are probably just fallout from that. – Marc B Jul 11 '13 at 15:27
  • I'm submiting the form via post method. and My form contain 'username' and 'password' as you can see. – Ahmad Ali Jul 11 '13 at 16:09

5 Answers5

0

You have display_errors turned off on one server and on on the other. The issues still occur on both servers, you just don't get told about them on one.

The issue is that you are accessing variables or parts of arrays that are not defined before you try to use them.

Anigel
  • 3,435
  • 1
  • 17
  • 23
  • I added ini_set('display_errors',1); error_reporting(E_ALL); at top of my views/login.php page (local server) and no error reported. and everything is fine. I checked other pages that have $_POST method. It seems that i have this problem in all of my pages in codeigniter. wherever we have $_POST, It doesn't work and I get same errors for all of my pages on web sever while all of other models,... works fine on local. It seems that i retrieve data correctly bud i can't send any data via $_POST method on whole website. http://caspiansang.ir/ http://caspiansang.ir/admin – Ahmad Ali Jul 11 '13 at 16:43
0

Remote server has display_errors option turned on and error_reporting option is set to include E_NOTICE.

The easy solution is to set display_errors to 0

The proper solution is to replicate server's settings on your local machine and fix the notices

JimiDini
  • 2,039
  • 12
  • 19
  • I did the inverse form of that, I added ini_set('display_errors',1); error_reporting(E_ALL); to top of my page but no error reported on local server. – Ahmad Ali Jul 11 '13 at 17:10
0

The $_POST superglobal array may not be having the username and password set. To resolve this proble, you could check if they're set with isset():

if(isset($_POST['submit']))
{

//code block

}

Turning on error reporting will help you find out what's wrong with your script. To do that, add this to the top of your script:

ini_set('display_errors',1); 
error_reporting(E_ALL);

To set this globally, you can edit your php.ini file and change the error_reporting directive to the following:

error_reporting = E_ALL

The headers already sent error message usually caused by whitespace before <?php or after ?>. For a detailed explanation and helpful tips, see this StackOverflow answer.

Hope this helps!

Community
  • 1
  • 1
0

You can also try using codeigniters input class $_POST['username']; you used PHP native post super global variable try $this->input->post('username') to see if this problem is solved

broswilli
  • 307
  • 2
  • 5
0

I found the solution. It was caused because mod_rewrite was enabled in htaccess so $_post didn't work.

thanks

Ahmad Ali
  • 1
  • 4