$_POST Empty with .htaccess - php

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.

Related

Changing the page URL and get first two letters

How can I change page url query and get first two letters . in .htaccess or php
http://127.0.0.1/site/flowers/index.php?query=Bird
http://127.0.0.1/site/flowers/index.php?query=eagle
Like this :
http://127.0.0.1/site/flowers/search/bi/bird.html
http://127.0.0.1/site/flowers/search/ea/eagle.html
Htaccess code : 127.0.0.1/site/.htaccess
RewriteEngine on
RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [L]
Here is a Javascript code that you can use for this redirection:
<html>
<head>
<title>Form Post Example</title>
<script>
function prettySubmit(form, evt) {
evt.preventDefault();
var val = form.query.value.toLowerCase();
window.location =
form.action + '/' + val.substr(0, 2) + '/' + val.replace(/ /g, '+') + '.html';
return false;
}
</script>
</head>
<body>
<form method="get" action="flowers/search" onsubmit='return prettySubmit(this, event);'>
<input type="text" name="query">
<input type="submit" value="Search">
</form>
</body>
</html>
Then you can have this rule in site/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/.]+)\.html$ index.php?query=$1 [L,QSA,NC]
This will convert your URLs to:
http://127.0.0.1/site/flowers/search/bi/bird.html
When you perform search using word bird in search field.
Add the following to your .htaccess
RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [R=301]
RewriteCond %{QUERY_STRING} query=([^/\.]{2})([^/\.]+)
RewriteRule index.php search/%1/%1%2.html [R=301]
Then http://127.0.0.1/site/flowers/Bird.html redirects to http://127.0.0.1/site/flowers/index.php?query=Bird which in turn redirects to http://127.0.0.1/site/flowers/search/Bi/Bird.html
I'm assuming this is what you want?

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.

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

integrating tiny url with codeigniter

I have PHP code for shortening a URL. I want to integrate it with CodeIgniter. How can I do this?
I have 3 pages: index.php, page.php and a .htaccess file and one database backup file.
index.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$url=$_GET['url'];
$url_details=mysql_fetch_array(mysql_query("select * from tinyurls where shorturl like ('".$url."')"));
if(!empty($url_details)){
header("location: ".$url_details['actualurl']);
}else{
echo '<h2 style="color:red">Error 404: Page not found</h2>';
}
?>
page.php
<?php
if(isset($_POST['url'])){
mysql_connect("localhost","root","");
mysql_select_db("test");
$exist=mysql_fetch_array(mysql_query("select * from tinyurls where actualurl='".$_POST['url']."'"));
if(empty($exist))
{
function get_random_word($length=6){
$chars='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$word="";
for($i=0;$i<$length;$i++){
$word.=substr($chars,rand(1,strlen($chars)),1);
}
$isexist=mysql_fetch_array(mysql_query("select id from tinyurls where shorturl like ('".$word."')"));
if(empty($isexist)){
return $word;
}else{
return "";
}
}
$tiny_word="";
while($tiny_word==""){
$tiny_word=get_random_word();
}
mysql_query("insert into tinyurls set shorturl='".$tiny_word."', actualurl='".$_POST['url']."'");
}else{
$tiny_word=$exist['shorturl'];
}
echo "TinyURL: http://".$_SERVER['SERVER_NAME']."/tinyurl/".$tiny_word."<br/><br/>";
}
?>
<html>
<head><title>Shorten URL</title></head>
<body>
<form method="post" action="">
Enter the URL:<br/>
<input type="text" name="url" style="padding:5px;width:500px"/><br/><br/>
<input type="submit" style="padding:5px" value="Shorten URL">
</form>
</body>
</html>
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^p\/(.*)$ page.php?q=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
How can I integrate it with CodeIgniter?
when iam integrating this with codeigniter the controller goes to the home page of my site not to the original url
In your code you're just echoing the URL instead of redirecting it:
header('Location: http://' . $_SERVER['SERVER_NAME'] . '/tinyurl/' . $tiny_word);

Track urls using php and mysql

How to track the page views and ip address of the short url this code is creating. thanks in advance.
PHP Code
<?php
//include database connection details
include('db.php');
//redirect to real link if URL is set
if (!empty($_GET['url'])) {
$redirect = mysql_fetch_assoc(mysql_query("SELECT url_link FROM urls WHERE url_short = '".addslashes($_GET['url'])."'"));
$redirect = "http://".str_replace("http://","",$redirect[url_link]);
header('HTTP/1.1 301 Moved Permanently');
header("Location: ".$redirect);
}
//
//insert new url
if ($_POST['url'] && $_POST['url'] != 'http://') {
//get random string for URL and add http:// if not already there
$short = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 5);
mysql_query("INSERT INTO urls (url_link, url_short, url_ip, url_date) VALUES
(
'".addslashes($_POST['url'])."',
'".$short."',
'".$_SERVER['REMOTE_ADDR']."',
'".time()."'
)
");
$redirect = "?s=$short";
header('Location: '.$redirect); die;
}
<h1> URL to shrink: </h1>
<form id="form1" name="form1" method="post" action="">
<input name="url" type="text" id="url" value="http://" size="75" />
<input type="submit" name="Submit" value="Go" />
</form>
<!--if form was just posted-->
<?php if (!empty($_GET['s'])) { ?>
<br />
<h2>Here's the short URL: <?php echo $server_name; ?><?php echo $_GET['s']; ?></h2>
<?php } ?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
You'll need to create a separate table for keeping a count of the hits and IP's your shortened URL's are getting. Create a table (let's call it "hits") containing url_id and ip_address as it's columns.
Inside your if (!empty($_GET['url'])) condition, make an insert into the hits table with the ID of the URL it has requested and the IP address the request came from.
How to track the page views and ip address of the short url this code is creating.
Enable logging in your webserver and then use a log file analyzer that will give you that information (most common logfile analyzers do have the info you're looking for).

Categories