portapipe / Login-GroceryCrud

A Login/Logout system for GroceryCrud (codeigniter).
32 stars 26 forks source link

Login failure #11

Closed RayofNogg closed 6 years ago

RayofNogg commented 6 years ago

I finally got this working and learned a lot about CodeIgnitor and GroceryCRUD :) Thanks to portapipe for writing this, it is a great looking login solution. Hopefully this will help someone else.

First problem in /views/login.php; Line 25 "=" where "php" should be. This kept it from working at all.

 <form class="form col-md-12 center-block" action="<?=base_url();?>login/makeLogin" method="post">
 <form class="form col-md-12 center-block" action="<?php base_url();?>login/makeLogin" method="post">

Second problem in /controllers/Login.php After a failed login, then entering the correct credentials and clicking Login we get a 404 file not found with the following in the address bar: http://mysite.com/CodeIgnitor/index.php/login/login/makeLogin Somehow there is an extra "/login" inserted. Reloading the login and entering the correct info works fine.

The problem is "$this->load->view('login.php', $data);" gets loaded again and corrupts the path data. I made the following changes to the "function makeLogin()" as follows:

/ ERROR PART / $data['error']= $loginConfig['Error Message']; // $this->load->view('login.php', $data); $this->session->set_flashdata('message', 'User Name or Password are wrong!');
redirect("/login");

Then in /views/login.php, added the following line right after the Login Page title: <div id="infoMessage" style="color:red"><?php echo $this->session->flashdata('message');?></div>

It is working like a champ now.

portapipe commented 6 years ago

Hi @RayofNogg , thanks :smile:

The <?= tag is not a mistake but a short tag that means <?php echo You don't have it enabled in your php.ini file for sure. (I strongly suggest you to enable it!)

For your second problem you have to edit the Login.php controller with your correct path, plus in the config.php file (the codeigniter one) you have to insert your main url to make all the project work as expected. The error message is customizable in the Login.php controller, like the md5 usage and the redirect url after login.

If you want to try it I'm sure it will work out of the box!

RayofNogg commented 6 years ago

portapipe, thanks for the fast response. I have only been using CodeIgnitor for four days and I'm no programmer, so forgive me of my ignorance.

Shortcode is on and server is configured to use one php.ini for all subdirectories (I'm using PHP v5.6 on Bluehost shared hosting).

Snipet from php.ini; ; This directive determines whether or not PHP will recognize code between ; <? and ?> tags as PHP source which should be processed as such. It's been ; recommended for several years that you not use the short tag "short cut" and ; instead to use the full <?php and ?> tag combination. With the wide spread use ; of XML and use of these tags by other languages, the server can become easily ; confused and end up parsing the wrong code in the wrong context. But because ; this short cut has been a feature for such a long time, it's currently still ; supported for backwards compatibility, but we recommend you don't use them. ; Default Value: On ; Development Value: Off ; Production Value: Off ; http://php.net/short-open-tag short_open_tag = On

I created the file test.php with the folowing contents

<?
echo "PHP Short Tag Test";
?>

and when ran, outputs;

PHP Short Tag Test

so it appears to be working, so I don't know why the original code isn't working for me.

As for configuration, I have the following in config.php;

$config['base_url'] = 'http://mysite.com/CodeIgnitor/';

$config['index_page'] = 'index.php';

Issuing echo site_url(); die(); in main.php Returns http://mysite.com/CodeIgnitor/index.php

I don't see a place in Login.php to put a base url. The only thing that looks like a path is here and it does redirect to markers upon successful login. $loginConfig = array( "Page After Login" => "main/markers/", "Error Message" => "Your Username or Password are incorrect!", "Use MD5 Encryption" => false, "Show Permission Management Tips" => true, //suggested ); Thanks for your help, Ray

portapipe commented 6 years ago

This could help you: https://stackoverflow.com/questions/1386620/php-echo-vs-php-short-tags

If you're using php 5.4+ the <?= tag is enabled by default.

In Login.php the redirect URL after login is "Page After Login"

If you want to remove the index.php file from the URL put this in your htaccess:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

and remove index.php from the codeigniter's config file.

Hope this will help you ;)

RayofNogg commented 6 years ago

FYI As it turns out, the reason the login.php was returning a 404 was not because the original shortcode echo wasn't working, but because it was. When I mistakenly changed the = to php, it effectively rendered the code inoperative as I didn't add the echo, but the login worked.

As a test, I changed it back to the original line here: <form class="form col-md-12 center-block" action="<?=base_url();?>login/makeLogin" method="post"> and it no longer worked, returning the 404.

I also tried replacing the

<?=base_url();?>

with

<?php echo base_url();?

and it also fails.

Of course completely removing the base url code also works fine as in the following line. <form class="form col-md-12 center-block" action="login/makeLogin" method="post">

Apparently CodeIgnitor is using the base url environment setting to alter the path, but I am not smart enough to know why, just that removing it the code makes it work. I also don't want to break GroceryCRUD by altering the recommended url settings.

Since it seems to be working fine now, I think I'll just leave it as is.

Thanks for all your work, it has solved a huge problem for me.

portapipe commented 6 years ago

The problem is that you need to remove the index.php from the URL. If you use <?=base_url()?>index.php/login/makeLogin it should not give 404.

The main structure of codeigniter is to pass all the data to the index.php page that use the next URL segment to point you to the controller (the Login.php controller) and the next segment again is for the single public function inside it.

If You leave the index.php you have to add it to your URLs and so to that href too but if you'll use htaccess to remove index.php from the URL, you'll just have to point your first segment to the controller. This is how you should do it, but if you like it you just have to use it in your links :)