Codeigniter cannot refresh page after session destroy and direct - php

I have a PHP program written on code igniter that needs your help! Have been trying for 3 weeks!
I have the htaccess mod rewrite to make http://www.sampleurl.com/controllername instead of http://www.sampleurl.com/index.php/controllername
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,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.
ErrorDocument 404 /index.php
</IfModule>
I have a controller for Dashboard (currently used for testing the session.)
public function index()
{
$is_logged_in = $this->session->userdata('fb_session');
$data = array(
'fb_data' => $is_logged_in,
);
if (!isset($is_logged_in) || $is_logged_in != true){
redirect('/error');
}
}
Below is the function that suppose to kill the current session and redirect to dashboard page.
$('#logoutbtn').click(function(){
$.ajax({
type:"POST",
url: "/fbcontroller/killsession",
datatype: "json",
success: function(){
alert("Logout");
}
});
});
public function killsession(){
$this->session->sess_destroy();
redirect('/dashboard');
}
Problem 1: as I redirect from function in 1 controller to another, the redirection fails here. Instead of directing to dashboard, the firebug displays 404 error, page not found. And in the response, it displays all the HTML code of /error page. Does it mean that the redirection works? If yes, why wouldn't it display on browser?
Problem 2: session destroyed, but logged in page stays even as I refresh (F5).

For htaccess I suggest:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
And know that when you kill session CI will create a new 1 on your next Page load (in your case on the redirect) .. Session isn't just for user logins.
Don't forget at app/config/config.php to set
$config["index_page"] = '';

Related

Rewrite URLs in RESTful API

I have to create an API in PHP, which I have never done before. I have a index.php in my root folder which contains a form. A button click activates an AJAX Request to read all entries and a form can be filled out to send data. I will also have DELETE and update.
ajaxcall.js
// GET to retrieve
var req;
req=new XMLHttpRequest();
req.open("GET", 'src/api/v1/posts',true);
req.send();
//post with ajax
$.ajax({
type:"POST",
url: "src/api/v1/posts",
data: test,
ContentType:"application/json",
success:function(){
alert('successfully posted');
},
error:function(){
alert('Could not be posted');
}
});
I have a folder src/api/v1 in there I want the endpoint file api.php which handles the different requests. I now need all the AJAX calls to be resend to api.php which sits inside that v1 folder. I created an .htaccess which is stored in also stored in src/api/v1. I added the following:
.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule src/api/v1/(.*)$ src/api/v1/api.php?request=$1 [QSA,NC,L]
Unfortunately I get a 404 not found, it just does not seem to re-write the URL to the api.php so it is looking for /posts which does not exists. What am I doing wrong?
Actually you should store the .htaccess on the root directory, otherwise Apache will not know that there's an .htaccess under /src/api/v1, so apache will look for the directory src/api/v1/posts and as is it's not found a 404 error will be returned.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule src/api/v1/(.*)$ /src/api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

CodeIgniter 3 Routing Issue

I used CI's routes and have the following entry in config/routes.php
if($subdomain == 'debug')
{
$route['debug/(:any)'] = "admin/debug/$1";
}
elseif($subdomain == 'dev')
{
$route['models/(:any)'] = "v2/dev/models/$1";
}
If I were to browse to
dev.mydomain.com/models/test/
everything works fine and I get the correct output.
However, I were to browse to
dev.mydomain.com/models/test/trip_id/9091
I will get a 404 Error page.
Only way to make it to work is to use the following route entry.
$route['models/(:any)/(:any)/(:any)'] = "v2/dev/models/$1/$2/$3";
What really confuses me further is that it works if I were to browse to
debug.mydomain.com/debug/test/trip_id/9091
What am I missing here as the route entries looks similar but will only work on the first subdomain (debug.mydomain.com) but not on the latter (dev.mydomain.com)
This is the content of my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help is greatly appreciated.
$route['models/(:any)'] = "v2/dev/models/$1";
Check the route path if you are calling the controller from the folder
it should be yourfolder/controller/method_name/$1

php, url hiding with the .htaccess

This is the real url:
http://web/app/index.php?id=1
Here I have used current .htaccess and working fine with me.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
Now the url is : http://web.com/app/index/i and working fine
but here is the problem the real url
http://web.com/app/index.php?id=1&name=abc
How can I set mention url with .htaccess for keep it short or any other good solution for hiding URL-.
is it possible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.
If we say, good practice is to rewrite everything to one index page, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :
http://example.com/app -> $uri = /app
http://example.com/app/sub-app -> $uri = /app/sub-app
but also query string applies to the $uri variable:
http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
in this case, if you want to examine just uri part before ? question mark:
$realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
next you can examine and manipulate $uri;
Example:
$trimmedUri = trim($realRri, '/');
if ($trimmedUri == 'app')
{
// you can show app page
}
elseif ($trimmedUri == 'contact')
{
// show contact
}
else
{
// show 404 page not found
}
// you can have some dynamic also also
if (is_file($filePath = 'custom-page/'.$trimmedUri))
{
include $filePath;
}
else
{
// 404 page
}
Final Code (regarding your current script)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#rewrite php file
RewriteRule ^([a-zA-Z]+)$ $1.php
#rwwrite with id
RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
#rewrite with id and name
RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3
I think what you're looking for is something like AJAX, mostly because of
but it could go to other pages while the url stay one url static.
in which case, you would looking at JavaScript and not php. You would need a way to quickly deal with the data and change it how you want it. I've recently started learning Angular, and it shows a lot of promise. Go through the tutorial yourself (just google angular.js and the tutorial is on their website), or some other JavaScript library (unless you want to hard-code everything, in which case, good luck) like jQuery or MooTools (names mentioned because they're libraries i have knowledge with). To my knowledge (I am no code guru) but a website's URI is what tells the server what to show you. You can make shortcuts
If, however, you want for it to just not have the .php filename, then I believe that
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.php\??(.*)$ $1.php&$2
Should, theoretically get you what you want.
Note: I can't test it right now, so it may error out.
EDIT: It does error out.

CodeIgniter Redirect on Heroku

I'm having some issues with CodeIgniter redirects on Heroku. Here is the URL:
http://sergei-game.herokuapp.com/
In particular, what is happening is that the redirects (/controller/function) are being appended to the URL rather than replacing /controller/function in the URL.
I do not have these issues when I navigate to the same URL on localhost.
Here is the relevant code in the main controller:
public function index() {
redirect('gameplayer/login', 'refresh');
}
public function login() {
if (isset($_SESSION['email'])) {
// if logged in, go to character selection page
redirect('gameplayer/viewAccount');
} else {
$this->load->view('auth/login');
}
}
Here is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I took a look at the Heroku logs, but they don't make very much sense to me.
You don't have the correct base URL configured in CodeIgniter. In the configuration file located at application/config/config.php find the line $config['base_url'] and set it to your URL. In your case it should be $config['base_url'] = 'http://sergei-game.herokuapp.com/';

PHP: htaccess default variable

I want to set a default variable for a query that comes at the end of a url
The .htaccess file redirects the url as follows:
http://www.server.com/folder/subfolder/index.php?page="some-page-name"
displayed url
http://www.server.com/folder/some-page-name
if the page name is not set, as in:
http://www.server.com/folder/
the default would be "index". I could use php function header("location:url") but that would display the "index" part at the end if the url... that I don't want.
htacess content
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
ErrorDocument 404 /error.html
ErrorDocument 403 /error.html
</IfModule>
You don't have to redirect to index.php. You can use something like:
header('Location: /folder/front-page');
If you just want http://example.com/folder/ show up your index page, you could use the following in your PHP script:
$requested_page = filter_input(INPUT_GET, 'pageID');
$allowed_pages = array('some-page', 'some-page-name');
if($requested_page == ''){
// display your index page as ?pageID is not set or empty
}
elseif(in_array($requested_page, $allowed_pages)){
// display $requested_page
}
else{
// display a 404 Not Found error
}
Try this code in your PHP file:
$pageID = 'index';
if(isset($_REQUEST['pageID']) && !empty($_REQUEST['pageID']))
$pageID = get_magic_quotes_gpc() ? stripslashes($_REQUEST['pageID']) : $_REQUEST['pageID'];
// Your code should now use the $pageID variable...
What this will do is set $pageID to a default value of "index". Then, if a different PageID is given by the mod_rewrite, $pageID will bet set to that value. Your code should then use the value of $pageID.
Your question was too long, didn't read, however if you just want to set a variable in your .htaccess file and pass it to PHP it's pretty easy to do it like this -
In the .htaccess file (http://httpd.apache.org/docs/2.0/mod/mod_env.html#setenv):
SetEnv default_url http://my.url/goes/here
In your PHP script (http://www.php.net/manual/en/reserved.variables.environment.php):
header('Location: '. $_ENV['default_url']);
Alternately, if you have ErrorDocument handlers, you might be able to simply send a status code as well (see the third option to header(), http://us.php.net/manual/en/function.header.php).

Categories