Loading page gives an error in CodeIgniter - codeigniter

When I login, it gives me "error page not found". It's not loading the controller function.
This is my view file:
<form name="loginform" action="welcome/login" method="post">
Enter User Name:
<input type="text" name="uname">
Enter Password
<input type="password" name="pass">
<input type="submit" name="login" value="Login">
This is my login controller file:
class Login extends Controller {
function Login()
{
parent::Controller();
}
function login()
{
$this->load->view('welcome_message');
}
}
This is my welcome controller file:
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->model('Loginmodel');
$this->load->view('welcome_message');
}
public function login()
{
$this->load->model('Loginmodel','login');
$info = $this->logn->login();
$this->load->view('welcome_message',$info);
}
}
Can you give me any idea what is wrong?

The code you provided seems fine, so I would guess it has to do with this:
<form name="loginform" action="welcome/login" method="post">
The path is relative, which is only going to work if there are no url segments present. If the current URL is /hello/world, you will end up posting to /hello/world/welcome/login
Try a full URL or absolute path. Example:
<form name="loginform" action="/welcome/login" method="post">
<!-- ^^^ Note the leading forward slash -->
Or use base_url() for the full url:
<form action="<?php echo base_url(); ?>welcome/login">
You may also be interested in the form_open() function which will do this automatically.
<?php echo form_open('welcome/login'); ?>
The only other thing I can see would have to do with whatever $this->login->login(); does. If you can confirm what your URL actually is when you see the "page not found" error, that would be a great help.

Related

Laravel custom login is failing for the first time with "route [login] not defined" error. Working fine in second attempt

I'm setting up my custom authentication system in my Laravel app. I've deleted all the default auth controllers and not using make::auth. And my auth is working properly. My main problem is that when I tried to log in for the first time, it's failing with "Route [login] not defined" error, but in second attempt, it's working properly. And if I repeat the process, it's continuing again and again like the first two attempt. Actually, I've never used login route anywhere.
Here is my form:
<form action="{{ url('/log-in') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
Here is my route:
Route::post('/log-in', 'AuthController#login');
Here is my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use App\User;
class AuthController extends Controller
{
public function login(Request $request) {
// Get current user.
$user = User::where('phone', $request->phone)
->first();
if ( Hash::check($request->password, $user['password']) ) {
Auth::login($user, true);
Auth::logoutOtherDevices($request->password);
return redirect()->back();
}
}
}
Use for route.
Route::post('/log-in', 'AuthController#login')->name('login');
Use your form.
<form action="{{ route('login') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
This is a tricky and annoying problem. you need to change your return from return redirect()->back(); to loading 1 known blade or a known redirect. Sometimes at login your route fails to set a back redirect. so you can try to set a return view() or a return to a known url. So for example if the login is success return to index , if not load error page.
Hope this helps

getting an error while submitting, stating 'object not found!'

I am very new to this laravel framework.
And I am stuck here. I got an error like 'Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.0.2o PHP/5.6.36'
that's my form:i am just trying to insert data in database
#extends('layouts.app')
#section('content')
<form method="posts" action="/posts">
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>
#stop
</body>
</html>
and my controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller
{
public function index() {
return "lets see whether it is working or not".$id;
}
}
public function create() {
return view('posts.create');
}
public function show(){
//return view();
}
public function edit(){
//
}
public function update(Request $Request,$id){
//
}
public function destroy($id){
//
}
public function store(Request $request){
Post::create($request->all());
}
}
and finally my route:
Route::resource('posts','PostController');
Change the method to 'post' and the action to {{ route('posts.store') }}. Also be sure to add the #csrf blade directive in your from to include the csrf token.
So your form will look like:
<form method="post" action="{{ route('posts.store') }}">
#csrf
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>

How to link to another page when clicking button

login.php (controller)
class login extends CI_Controller {
public function index()
{
$this->load->view('login');
}
public function Click()
{
$action = $this->input->post('register'); // $_POST['start']; also works.
if($action)
{
$this->load->view('register');
}
}
}
login.php(views)
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<input type="submit" id="submit" class="form-control" value="Buat Akun" name="register" >
</div>
</form>
how can I change my view to register.php after clicking register button. The error is I keep back to login page after clicking register.
You can try this :
In Login controller :
public function register()
{
$this->load->view('register');
}
In view:
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<a href="<?php echo base_url('login/register');?>" id="submit" class="form-control" name="register" >Register</a>
</div>
</form>
You need to enable mod_rewrite module in apache
Please follow below mention step to enable mod_rewrite module:
1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.
You can try this :
In Login Controller:
class Login extends CI_Controller {
public function index()
{
if($this->input->post('login') == 'Login'){
$this->load->view('login');
}else{
$this->register();
}
}
public function register(){
$this->load->view('register');
}
}
In View :

Post variables in Codeigniter

I have a view in Codeigniter that is a form written in HTML directly.
<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">
<label for="usuario" id="usr">Usuario</label>
<input type="text" name="usuario" /><br /><br />
<label for="contrasenya" id="ctr">Contraseña</label>
<input type="password" name="contrasenya" id="ctr" />
<label for="acceder"></label><br /><br />
<input type="submit" name="acceder" id="bacceder" value="Entrar" />
</form>
The form has two fields: usuario (user) and contraseña (password).
And it is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cindice extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('indice');
}
public function mostrar()
{
$mostrar=$this->input->post('usuario');
print_r($_POST);
echo "<br />";
}
}
?>
When I load the page localhost/index/cindice and I write a username and a password. But the result of print_r($_POST) command is array(), what it means that any value was saved in the form.
How can I fix it?
I think it is empty because you have spelled the the method attribute wrong in your opening form tag. You have put
<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">
When it should be
<form action="http://localhost/index.php/cindice/mostrar" method="post">
Be mindful if you have CSRF switched on in your config - if it is then you need to use the codeigniter form helper and form_open() so that the hidden security value is added.

Codeigniter: simple form function

I'm stuck writing a simple form...I feel dumb.
Here's my controller:
function welcome_message(){
//Update welcome message
$id = $this->session->userdata('id');
$profile['welcome_message'] = $this->input->post('welcome_message');
$this->db->update('be_user_profiles',$profile, array('user_id' => $id));
}
And the html:
<?php print form_open('home/welcome_message')?>
<input type="checkbox" value="0" checked="false">Don't show me this again</input>
<p>
<input class="button submit" type="submit" class="close-box" value="Close" />
</p>
<?php print form_close()?>
Edit
I simply need it to submit to a private function and return to the home page (page submitted from).
You have to name your input...?
<input type="checkbox" value="0" name="welcome_message" checked="false">
Don't show me this again
</input>
EDIT: You can't submit to a private function via the URL (in a default CI installation, anyway).
Do it like this:
function welcome_message()
{
if(isset($_POST['welcome_message']))
{
$this->_my_private_function();
}
}
private function _my_private_function()
{
// do your thing with the $_POST data
redirect('your/uri', 'location');
}
But you really don't have to redirect if your returning to the same page (controller method) that called the private function in the first place. Would be a waste.

Resources