this controller login using ajax. login.php
<?php defined('basepath') or exit('no direct script access allowed'); class login extends ci_controller { public function index(){ $this->load->view('login'); } public function checklogin(){ if($this->input->is_ajax_request() && isset($_post['email']) && isset($_post['password'])){#restrict direct access browser if(empty($this->input->post('email')) || empty($this->input->post('password'))){ echo "not"; }else{ if(filter_var($this->input->post('email'), filter_validate_email) === false){ echo "not"; }else{ $data = array( 'email' => $this->input->post('email'), 'password' => md5($this->input->post('password')) ); if($this->db->select('*')&& $this->db->from('users')&& $this->db->where($data)){ if($this->db->count_all_results() > 0){ redirect('homepage'); #redirect }else{ echo "not"; } } } } }else{ show_error("no direct script access allowed"); } } }
this javascript file:
$(document).ready(function(){ $("#login-form").submit(function(event){ event.preventdefault(); $.ajax({ url: 'http://citest.local/login/checklogin', type: 'post', data: { email: $.trim($("#email").val()), password: $.trim($("#password").val())}, }) .done(function(result) { if(result == "not"){ $(".has-feedback").addclass('has-error'); $(".glyphicon").addclass('glyphicon-remove'); }else{ $(".has-feedback").removeclass('has-error'); $(".glyphicon").removeclass('glyphicon-remove'); $(".has-feedback").addclass('has-success'); $(".glyphicon").addclass('glyphicon-ok'); console.log(result); } }); }); });
this result returned console redirect function controller:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>simple contacts</title> <link rel="stylesheet" href=""> </head> <body> </body> </html>
.htaccess
<ifmodule mod_rewrite.c> rewriteengine on rewritebase / # disable rewrite valid directory/files rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d #map request urls specific controller method rewriterule ^(.*)$ index.php?/{controller}/{method}/$1 [l] </ifmodule> <ifmodule !mod_rewrite.c> # if don't have mod_rewrite installed, 404's # can sent index.php, , works normal. # submitted by: elliothaughin errordocument 404 /index.php </ifmodule>
site config (nginx)
server { listen 80; root /home/user/trytry/ci_test; index index.html index.htm index.php; # make site accessible http://localhost/ server_name citest.local; # unless request valid file (image, js, css, etc.), send bootstrap if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?/$1 last; break; } location / { # first attempt serve request file, # directory, fall displaying 404. try_files $uri $uri/ =404; # uncomment enable naxsi on location # include /etc/nginx/naxsi.rules } # nginx-naxsi used nginx-naxsi-ui : process denied requests #location /requestdenied { # proxy_pass http://127.0.0.1:8080; #} #error_page 404 /404.html; # redirect server error pages static page /50x.html # #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root /usr/share/nginx/html; #} # pass php scripts fastcgi server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # note: should have "cgi.fix_pathinfo = 0;" in php.ini # php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } # deny access .htaccess files, if apache's document root # concurs nginx's 1 # #location ~ /\.ht { # deny all; #} }
why redirect not working supposed be? returns string instead of redirecting url. please help! :(
ajax isn't designed work that. redirect code
redirect('homepage'); #redirect
is redirecting ajax request page, getting information homepage view, , passing ajax response.
if want redirect, should pass success or fail message via ajax, , use javascript redirect within browser
Comments
Post a Comment