How do I sign in and log in?

2

I have a HTML page with a button for admin before you have a popup login and the php of that login to is in another file. And I wanted to know how to do it so that if he accepts the login, he can always go to that button without having to always log in.

I know I need to do this with session but I do not know how to do it and I can not figure out what sites I've been.

Can someone tell me a website that explains how session works or even explain?

Here is the following code for the popup login:

$(document).ready(function() {
	$('a.login-window').click(function() {
		var loginBox = $(this).attr('href');
		$(loginBox).fadeIn(300);
		var popMargTop = ($(loginBox).height() + 24) / 2; 
		var popMargLeft = ($(loginBox).width() + 24) / 2; 
		$(loginBox).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300);
		return false;
	});

	$('a.close, #mask').live('click', function() { 
	$('#mask , .login-popup').fadeOut(300 , function() {
	$('#mask').remove();  
	}); 
	return false;
	});
});
<div id="login-box" class="login-popup">
  <div id="fechar">
    <a href="federados.html" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close"></a>
  </div>
  <form method="post" class="signin" action="php/login.php">
    <fieldset class="textbox">
      <label class="username">
        <span>Nome de Utilizador:</span>
        <input id="username" name="username" type="text" autocomplete="on" placeholder="Username" required>
      </label>
      <label class="password">
        <span>Palavra-Passe:</span>
        <input id="password" name="password" type="password" placeholder="Password" required >
      </label>
      <br/>
      <input type="submit" class="submit_button" value="Iniciar Sessão"/>
      <p><a class="forgot" href="#">Forgot your password?</a></p>
    </fieldset>
  </form>
</div>

PS: The code does not work because it's a popup only works if you click the button.

    
asked by anonymous 17.06.2015 / 17:13

1 answer

3

I usually use Cookies , LocalStorage or SessionStorage to do this kind of thing.

LocalStorage and SessionStorage are HTML5 features and are very simple to work with. If you want the session to die after closing the browser just use the SessionStorage, otherwise use the LocalStorage that even when closing the browser the user will remain logged in.

The big problem of these two storages is that old browsers do not fully support it, so you need to evaluate if your user will not use an IE7 from life ...

Cookies can be useful if you want a compromise between SessionStorage and LocalStorage, since you can set a "validity" for the Cookie.

One advantage of Cookie is that it is a feature supported by older browsers, it's just a bit annoying to read and write values. I usually use a Cookie.js that I found on the internet, let me know if you need what I can include in the answer.

Anyway, you can search a little more about each feature I've gone through and decide on the best strategy.

Then you could basically do the authentication system as follows:

  • When authenticating, the server generates a token or some kind of unique identification for you and returns it;
  • You get this return and write it in a cookie, localstorage or sessionstorage;
  • Whenever the user navigates, you should retrieve this value, send it to the server and if it is valid, navigate to the login screen or otherwise.
  • When logging off, simply delete the sessionstorage, localstorage or cookie values and invalidate the token or unique identifier on the server.
  • I hope I have helped.

    EDIT

    PHP offers the global $ _SESSION that can also help you. So it's up to you.

        
    17.06.2015 / 18:43