CodeIgniter htaccess redirect url issues - php

I want "nice urls" for my website. I'm looking at using .htaccess and I have tried many versions.
Now I have this:
IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
So this removes index.php from the URL. At first it was ok, but
Here is my config.php:
$config['base_url'] = 'localhost/mysite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
And my .htaccess file is in my app root.
folder img
I need a login for admin page, so localhost/mysite/admin is the my url for admin site.
public function index()
{
// $this->session->unset_userdata('logged_in'); session_destroy();
$data['sitename']="Varkocs Old Pub";
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('admin/home_view', $data);
}
else
{
//If no session, redirect to login page
//redirect('login', 'refresh');
$this->load->helper(array('form', 'url'));
$this->load->view('admin/login',$data);
}
}
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('', 'refresh');
}
This is my admin controller. I try here if logged in I load home_view, else the login view.
<div id="" class="container-fluid ">
<div id="" class="row">
<div id="" class="col-md-4 text-center admin_container">
<h2><?php echo $sitename;?><h4>belépés az admin felületre</h4></h2>
<?php echo validation_errors('<p class="bg-danger">','</p>');?>
<form action="verifylogin" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="username">Username:</label>
<input class="form-control" placeholder="" type="text" size="20" id="username" name="username"/>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input class="form-control" placeholder="password" type="password" size="20" id="passowrd" name="password"/>
</div>
<input class="btn btn-warning btn-lg btn-block" type="submit" value="Login"/>
</form>
</div>
</div>
This is the login view. I can't use here:
echo form_open('verifylogin');
Because it's returning the wrong URL.
So I write HTML:
<form action="verifylogin" method="post" accept-charset="utf-8">
The form validation with CodeIgniter is working. But the redirect afterwards doesn't work.
$this->load->helper(array('form', 'url'));
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$data['sitename']="Varkocs Old Pub";
$this->load->view('admin/login',$data);
}
else
{
//Go to private area
redirect('admin', 'refresh');
}
I have tried many ways to fix this, but I failed.

The Codeigniter documentation says the following about the redirect function:
In order for this function to work it must be used before anything is outputted to the
browser since it utilizes server headers.
This rule can be tricky to follow. A space or line end before the opening PHP tag will already break it.

Related

.htaccess rewrite url , dont work on Form Submit

I am working on a search display, but got some problems with the htaccess, not working the way I want it to. I got multiple rewrites on the same url and I assume thats why. But i am not sure..
UPDATE: From what I can see by testing, all the Rewrite urls work. But the form force the page over to search.php?search=test and not search/test as i want.
So the real question is:
How do I get form action="" to submit to search/test/ and not search.php?search=test
What i get:
search.php?page=1
search.php?type=DEFINED_TYPE&search=test
search.php?type=DEFINED_TYPE&search=test&page=1
What i want:
search/1
search/DEFINED_TYPE/test
search/DEFINED_TYPE/test/1
Form:
<form action="" method="get" id="searchForm">
<label for="DEFINED_TYPE_radio">
<input type="radio" name="type" id="DEFINED_TYPE_radio" value="DEFINED_TYPE"<?php if($_GET['type'] == 'DEFINED_TYPE') echo ' checked' ?> />
<em<?php if($_GET['type'] == 'DEFINED_TYPE') echo ' class="selected"' ?>><i class="fas fa-tags"></i>Type</em>
</label>
<div class="input">
<input type="text" placeholder="Search here..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>" name="search" id="searchField" autocomplete="off" autofocus>
<i class="fa fa-search submit" id="submit"></i>
</div>
</form>
Htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^search$ search.php [NC]
RewriteRule ^search/([^/]+)$ search.php?page=$1 [NC]
RewriteRule ^search/([^/]+)/([^/]+)$ search.php?type=$1&search=$2 [E=ORIG_URI:/$1]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)?$ search.php?type=$1&search=$2&page=$3 [E=ORIG_URI:/$1]
Anyone who can spot what I do wrong?
Note that as you are using get method on the form, every field is added with its name to the url as a param.
I suggest you submit the form with method post and add the url in the action attribute (or the whole url):
<form action="search.php?search=test" method="post" id="searchForm">
or
<form action="/search/test" method="post" id="searchForm">
That involves changing how you read those values

Codeigniter no data sent posted on form

My login system suddenly stopped working, I can't fix it, I've been looking into it and it seems that the data doesn't get posted back but I can't find the problem.
This is my view:
<?php if(! is_null($err)) echo "<div class=\"alert alert-danger\">".$err."</div>";?>
<form class="form-signin" action="<?php echo site_url("user/validate"); ?>" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="email" class="sr-only">Email address</label>
<input type="email" name="email" class="form-control" placeholder="Email address" required autofocus>
<br/>
<label for="password" class="sr-only">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" required>
<br/>
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign in</button>
</form>
</div>
This is my my validate function inside the user controller:
public function validate()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
// Load the model
echo "$email and $password sent";
print_r($this->input->post());
exit();
$this->load->model('User_model');
// Validate user's login details
$result = $this->User_model->validate($email, $password);
// Now we verify the result
if($result == 0){
// If user did not validate, then show them login page again
$err = 'Invalid email and/or password.';
$this->login($err);
}else{
// If user did validate send him to homepage after setting the credentials
$this->session->set_userdata($result);
redirect(site_url("/index/index"));
}
}
the echo and the print_r show that no data is posted.
After some research I found out that the problem should be the .htaccess file, this is my current .htaccess:
RewriteEngine On
RewriteRule ^(application) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
But it's not working.
you have an exit(); in validate function comment it
try : if(!empty( $err ))
try : base_url("index.php/user/validate");
try : load model inside
public function __construct()
not inside of
public function validate()
<form class="form-signin" action="<?php echo site_url("user/validate"); ?>" method="post">
Change this to the following:
<form class='form-signin' action="<?php echo base_url();?>user/validate" method="post" name="user_from">
Also don't use the submit button name as 'submit'. Just use sbt or other name, id too...

Form action redirect to http//localhost/xampp not to the specified location

I'm new to CodeIgniter. I have a homepage with form controller form. I want when click on submit in homepage redirect to signup function in controller form
But it redirect to http//localhost//xampp
This is Home_view view
<?php $id = 1; ?>
<form action="<?php echo base_url('login_c/signup/' . $id); ?>" method="post" >
<input type="submit" value="Sign up" />
</form>
This is form controller
public function index()
{
$this->load->helper('url');
$this->load->view('Home_view');
}
public function signup($id)
{
echo$id;
$this->load->view('myproject/Signup_v');
}
Yes problem may be with your .htaccess. Try with this once,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

PHP: Validating Form When Routing

I'm trying to validate a form that's being submitted through a PHP router.
All my traffic is sent to index.php via htaccess. to index.php:
RewriteEngine On
RewriteBase /router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /divbase/index.php?/$1 [L]
index.php then provides the appropriate content:
<?php
Router::get('/form', function() {
include 'content/form.php';
});
?>
so when you go to example.com/form, the form page is loaded:
<?php
$errorMessage = false;
if (isset($_POST['submit'])) {
if (!isset($_POST['name']) || $_POST['name']=='') {
$errorMessage = 'Please enter your name';
}
else {
// do something with the data
echo "Success!!";
}
}
?>
<form method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>
However, when I try to submit the form on that page it 404s. After doing some troubleshooting, it looks like the PHP thinks that the form is being submitted to index.php rather than form.php.
I've tried to set the form action to:
<?php "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
But it still 404s. Does anyone have any suggestions for this?
You need to add another route for POST, for example...
Router::get('/form', function() {
include 'content/form.php';
});
Router::post('/form', function() {
echo 'Hello world';
});
Your form is using method="post" so you need to add a POST route

$_POST Empty with .htaccess

I was searching now quite a bit on the internet but can not find any solution.
I have an .htaccess file, which rewrites my query string into the GET variable.
This works absolutely fine.
when it comes to the POST variable, it is the first time I get into this trouble.
I have used this .htaccess file already a lot of times on different servers, but now one of my client's server doesn't want to work.
When ever I post a form the whole POST variable is empty... I will post my .htaccess file, any help is appreciated.
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^pages.*$ /error/404 [L]
RewriteRule ^classes.*$ /error/404 [L]
RewriteRule ^templates.*$ /error/404 [L]
RewriteRule ^common.*$ /error/404 [L]
RewriteRule ^([A-Za-z0-9-_]+)/?$ /?p=$1&s=index&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&o=$5&%{QUERY_STRING} [L]
ErrorDocument 404 /error/404.php
the form looks like this
<div class="login">
<form action="/administrator" method="post">
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>Username</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="passwort" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Anmelden" /></td>
</tr>
</table>
</form>
</div>
and the login php file like this
<?php
session_start();
if( isset($_SESSION['login']) && $_SESSION['login'] === TRUE ) {
header('Location: /administrator');
exit;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$passwort = $_POST['passwort'];
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['PHP_SELF']);
if ($username == 'xxx' && $passwort == 'xxx') {
$_SESSION['login'] = true;
header('Location: /administrator');
exit;
}
}
?>
Your RewriteRule are active for GET, not for POST.
check if your /administrator script (why haven't you called it directly as administrator.php?) is called or create a rule for its access. Check in the access log to be sure you're calling the script when you post to it.
Add something like :
RewriteRule ^administrator administrator.php [L]
or RewriteRule ^administrator pathtoyourscript/administrator.php [L]
check without session to be sure it's not interacting badly with it.
You can use var_dump($_POST) to display all data. If there is no display, you've not called the right script or your path is wrong.

Categories