Addressing Codeigniter before and after login - codeigniter

i need to make a login system for my school job, and i want my url on address bar before login and after still the same. Just like facebook, there is url : facebook.com if we're not logged in, and there will be stil facebook.com if we're logged in.

Use HTML5 history API, jQuery,
refer this code examples
example

People use backbone for a one-page app generally.
I assume you are using an authentication library or an authentication system of your own in CodeIgniter.
Inside your default controller you should do something like this (the algorithm)
If user is logged in:
call the layout for the login system
else:
call the main page to be displayed when the user is logged in
For adding other information/pages to it, you could use jquery to replace the body text with the new page.
Where is the new page called from? It can be called from valid CodeIgniter Controllers which provide only the body html.
For example, in your main layout, you could use:
$(document).ready(function(){
$('#button1').click(function(){
$.get("mysite.com/second_page",function(data,status){
$('#main_div').html(data);
});
});
});

Related

Redirect two pages back with url()->previous() in laravel

I'm creating an affiliate tracking system with some custom affiliate links. These links trigger a controller function to store some metrics in the database then redirect to the actual page the user intended.
My desired function is this: insert affiliate link into webpage->user clicks->controller fires/data stored->redirect to intended page. One of the pieces of data I want to store is the originating site the user clicked on the link. But because I'm calling the controller with a URL, url()->previous() gets my affiliate link that fired the controller instead of the originating site that has the link embedded.
I'm not sure if "previous()" can accept parameters but i tried "previous(2)" and that obviously didn't work.
$url = url()->previous();
$click->came_from = $url;
Using the code above, if my link was hosted on www.w3schools.com and my affiliate link is myurl/affiliatelink/2, I would want "www.w3schools.com" to be inserted into my database, but i'm getting "myurl/affiliatelink/2" inserted instead. Can anyone help me figure out a way around this?
The previous() method returns the user's last location within the app, not the last URL the browser accessed.
To do that, in Laravel, you can try Request::server('HTTP_REFERER'), or in plain PHP, $_SERVER['HTTP_REFERER'].
That said, be aware that HTTP_REFERER is not exactly the most reliable value. It can be spoofed (faked) easily or not provided at all, so test accordingly.
If possible, the best option would be to add a GET parameter to the link present in the remote site, so that when you receive the request in your Controller, you have the means to identify where the user is coming from.
You can simply get and store the previous url with the parameter in a variable($url) from your blade file, then pass the variable to your anchor tag as shown below:
#php
$url= url()->previous();
#endphp
href="{{url($url)}}"
This works very fine.

how to create middleware in laravel

I have created the project which has login page login.blade.php which has only Regno to login-in.
once it login is the success it will redirect to home.blade.php and it followed by 4 more pages when clicking of next button in each page
page2.blade.php,page3.blade.php,page4.blade.php,page5.blade.php and finally, it should redirect to login page itself. The problem is the browser back button should not allow it to go back to the previous page.
can anyone suggest me how to create middleware for this?
thank you.
The browser's back button is not controlled by the server, where Laravel runs. There is no way for you to block it or interact with it in any way inside your PHP code.
What you can do is send XMLHHTTPRequest (XHR) with JavaScript. They ensure that on a button click you will stay on the same page in the browser and then your JS-code can read the server response (your rendered page templates) and replace the current content with the one from that response. You can read more on this when you google for AJAX, e.g. this article: https://www.sitepoint.com/use-jquerys-ajax-function/
Read this thing. I once implemented in the same way. By setting cache control in response header.
https://stackoverflow.com/a/51821808/10239067

Using securesocial services without using its views

I started integrating SecureSocial in my play/scala app, but I don't really like all the redirects it does between it's different views.
example - try to login from it's default login page and if you put in a wrong pass you will be redirected to a different page (url) but with the same login form. the only thing that is different is that there is an error message...
I want a simple login form (user/password provider) at the corner of my main page that submits it's data using ajax, this data is validated on the server and a response is made to either display error message/s or change the window.location.
Next to this form I will put a link to go to a more advanced login page that adds the option to use other providers like fb/twitter etc..
But from that page I also want to use ajax to submit the details and get the response.
I tried to browse into the SecureSocial source but got a little lost in there.
Can any one give me an idea how to use SecureSocial's but without using any of it's views?
NOTE: I'm not interested in customizing their views, It's not just a CSS/design issue, I want to handle the login details Ajaxly and not with normal form submission followed by redirects...
After some more rummaging around in SecureSocial code I got a better understanding of how it operates.
You can use any of the providers you listed in the play.plugins file seperatly to authenthicate the user's info from your own login/auth code. just make sure you send the right parameters that the provider needs.
I liked the way SecureSocial's ProviderController class dynamically decided what provider to use, based on a parameter. But I didn't like the responses it made - redirect.. I wanted to respond to an ajax request with some data and let the client side js handle it.
This is my solution:
pretty much copy all of ProviderController code to my own Auth.scala file (a Controller).
Changed the redirects related to "case ex, case _", kept the redirect on successful auth as it adds the SecureSocial session key related to the user.
Removed all the SecureSocial related routes from my routes file.
Put an additional hidden field with the logintype (userpass/google/fb/etc...) and configured my login ajax post to sent this along with the post to my Auth controller.
If you need more info comment here and I'll edit the answer.

MVC sitemap provider

In mvc sitemap provider I want to show a list menu's based on the id of the logged in user.I was thinnking about dynamicnodeprovider but it is working for the first request it self that is before logging in.How can i achieve this?
Thanks,
Ajai
You could use cookies to achieve that. Set a cookie when the user logs in and read it when he comes back. Obviously this wouldn't work if:
the user blocks cookies
the user visits the site for the very first time
so you would need a default view for first visits; i don't think you can avoid this problem.
You could take the approach of making a JQuery AJAX call to a controller method which returns the required sitemap information. This enables you to update the sitemap whenever you like based on client side events.
e.g. see this post : http://joelabrahamsson.com/entry/xml-sitemap-with-aspnet-mvc
Another decision is if you want the controller to return the sitemap as ready generated HTML and dynamically replace it in the DOM. Or ( more work ) return the pure sitemap in XML and have JQuery generate the SiteMap markup for you.

browser autocomplete/saved form not work in ajax request

Its really hard to search the any combination of keywords in search engine about this because it used by most popular developer wanted a custom autocomplete by ajax.
Most developer search about the custom autocomplete to get result from db by ajax or about how to disable browser autocomplete due to security reason or they wantted to use another autocomplete extender.
However I am not talking about the autocomplete. I finding about simple normal browser autocomplete or browser saved form either IE or FF that will act like dropdown recent choice during filling a text in a textbox.
It simple and normal filling a form like username and password in a login form. After the form submitted (the form data post) browser will save the autocomplete or event in FF will ask to save along with the password.
Now, think about the login submitted via ajax. The form data not automatically saved by either IE or FF simply because the form not sent by post method. I am pretty sure it is because of ajax vs post method.
CMS like DotNetNuke using this way and its really hard to me to type username and password for 5 user login for development purpose, event I want to let user save their own form data in the browser without any custom or extender. By another example, user can see and use same email to fill an any email form across web site or domain.
How to workaround with this?
Did you have suggestion what keywords is more suitable to search?
CallMeLaNN
I'm having the same problem. I was able to solve it for FireFox by adding a hidden iframe that I submit via JavaScript before doing my AJAX post. I still haven't found anything that works in Chrome/IE.
I've been faced with same issue and searched a bit. I think the solution below is the most convenient way to solve this if you have a login page. If we consider the login submitted via ajax, none of the browsers remember or offer autocomplate feature for user name and password field additionally ask to remember the credentials. But if you use the javascript submit feature (Probably it's not compatible with older versions of browsers), All of the browsers offers to save the username and password except IE. But I've found another javascript tricky for IE to make it offer to save username and password.
In my login page, I've handled the username and password and send them to serverside by ajax request and if the login is succeeded, i submitted the form by the method below otherwise It had been shown an Alert box to the user that the login was failed.
Please Check the link below:
[EDIT]: Link is broken
There is a fixed page about this issue in the page linked, i can not give you another link because of my reputation. Please search for the quotation below in the page:
Look at the fixed page.
Of course,this approach does not fit if you have a login section in the default page because of the form submitting. This causes the page flickering. I wonder if someone has an idea about it?
Here is some unobstrusive js jQuery code that will submit a form both via ajax ($.post method) to a real backend script and also to a dummy script via an iFrame, so the browser will save the submitted data for subsequent autocompletion.
This is working great under chrome. Any feedback is more than welcome!
var formframesindex = 0;
function onSubmitAjax(evt){
var $form = $(this);
var framesubmitting = $form.hasClass('framesubmitting');
var action = $form.attr('action');
var original_action = action;
if(!framesubmitting){
$.post(action,$form.serialize()+"&ajax=1", function(responseText,message,request){
formResponseHandler(responseText);
}, "json");
formframesindex++;
var formframe = $("<iframe name='formframe_id_"+(formframesindex)+"' id='formframe_id_"+(formframesindex)+"' class='formframe' src='/fakeformreceiver.php'></iframe>");
$('body').append(formframe);
var target = $form.attr('target');
$form.data('originaltarget',target);
$form.data('originalaction',original_action);
$form.attr('target','formframe_id_'+formframesindex);
$form.attr('action','/fakeformreceiver.php');
$form.addClass('framesubmitting');
$form.submit();
} else {
var current_target = $form.attr('target');
var original_action = $form.data('originalaction');
var original_target = $form.data('originaltarget');
var $frame = $('#'+current_target);
setTimeout(function(){
if($frame && $frame.length){
$frame.remove();
}
$form.attr('action',original_action);
$form.attr('target',original_target);
$form.removeClass('framesubmitting');
},100);
}
return framesubmitting;
}

Resources