PHP search form not working with .htaccess - php

I have search country form with code:
<form name="form_search" id="form_search" class="form1" method="get" action="country/search"><div class="row">
<div class="col-sm-4 col-md-7">
<label>Select country</label>
<div class="select1_inner">
<div class="form-group">
<select class="select2 select" style="width: 100%" name="q" id="q">
<?php
$sql = "SELECT * FROM country";
$query = $conn->query($sql);
while ($row = $query->fetch_assoc()) :
$country_name=$row['country_name'];
?>
<option value=<?php echo $country_name?>><?php echo $country_name?></option>
<?php endwhile;?>
</select>
</div>
</div>
</div>
At the frontend url will show : http://example.com/country/search?q=parameter
but I would redirect that url to real directory at the backend http://example.com/page.php?module=search&id1=$1
with .htaccess
RewriteEngine On
Options -Indexes
rewriteRule ^country/search$ page.php?module=search&id1=$1 [L]
But this is not working I can't get value q parameter
Please help me how configure .htaccess for this form
Thanks.

You should use RewriteCond backreferences:
RewriteCond %{QUERY_STRING} q=([^&]*)
RewriteRule ^country/search$ page.php?module=search&id1=%1 [R=301,L]
Line 1: test if query string containing q={any_characters}
Line 2: if the url is country/search, redirect to page.php?module=search&id1={any_characters} permanently

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 : error 403 after submit form

When I try to send my form with the base_url() method (so I click on the submit button), instead of the result of my request, I have a 403 error on the other page (forbidden acces), on the page generated by my controller.
I use a Wampp Server, the version 3.1.8 of CodeIgniter. I've also add url in the autoload file.
I have .htacess file with that :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
View page :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="<? echo base_url();?>form_validation">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Can you help me ? Thanks a lot
Hope this will help you :
NOTE : Assuming that default controller is welcome in route.php if not replace welcome with yours
In controller : you have method form_validation and you give in form action only validation :
public function form_validation()
{
echo 'OK';
}
Your form view is just like this :
REPLACE :
/* note missing php error in action*/
<form method="post" action="<? echo base_url();?>form_validation">
With :
/*add controller name also in action,
assuming your default controller is Welcome*/
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
View should be like this :
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
route.php should be like this :
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
NOTE ALSO : .htaccess should be in project folder , in your case CI folder

How to modify query string to plain url using PHP?

I have following html form :
<div class="row form">
<div class="col-md-offset-3 col-md-6 col-md-offset-3">
<form class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
<div class="form-group col-md-10 no-p-m">
<select class="form-control" id="basic" name="location" required>
<?php
$get_location = mysqli_query($conn, "SELECT * FROM product_sub_area");
if(mysqli_num_rows($get_location) == 0 ) {
$choose = 'No City found';
} else {
$choose = 'Choose City';
}
?>
<option value=""><?php echo $choose; ?></option>
<?php
while($get_location_result = mysqli_fetch_array($get_location) ) {
$location_id = (int) $get_location_result['psub_area_id'];
$location_name = htmlspecialchars($get_location_result['psub_name']);
echo "<option value='$location_id'>$location_name</option>";
}
?>
</select>
</div>
<div class="form-group col-md-2 no-p-m" id="basic">
<input type="submit" class="btn btn-default filter-search" value="BROWSE">
</div>
</form>
</div>
</div>
When I submit this form the url is showing like bellow :
http://localhost/freelancer/happiechef/location?location=1
But I need to show this type of url :
http://localhost/freelancer/happiechef/location/carlifonia
here /location/ is a page called location.php. I am hiding page extension using .htaccess.
So from location.php page I want to get $_GET['location'] value = $location_id
for eg.
$_GET['location'] = 1 or 2 ( select tag option value)
Existing .htaccess code
ErrorDocument 404 not-found.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/([\w-]+)$ location.php?location=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Here is an example of how this can be accomplished in jQuery:
First, add an id to your <form> tag so you can access it easily:
<form id="formname" class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
You also have a duplicate id called "basic". Make sure you leave it only on the <select> tag and remove the other one.
Then in your JS:
$("#formname").submit(function(event) {
event.preventDefault();
var loc = $("#basic option:selected").text();
window.location.href = '/freelancer/happiechef/location/' + loc;
});
This will forward the user to the URL, for example http://localhost/freelancer/happiechef/location/carlifonia. Then .htaccess will rewrite this URL to location.php?location=california.

CodeIgniter htaccess redirect url issues

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.

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...

Categories