Jump to content
php.lv forumi

jquery/ajax izveidot sesiju!


Recommended Posts

index.php

<form id="login_form" method="post">
             <ul>
               <li>
                 <label for="username">Username: </label>
                 <input type="text" name="username" class="field required" id="username" minleght="3" maxlength="10" />
               </li>
               <li>
                 <label for="password">Password: </label>
                 <input type="password" name="password" class="field required" id="password" onKeyPress="return capsLock(event)" />
               </li>
               <li>
                 <center>
                   <input id="submit_login" type="submit" value="Authenticate" />
                   <div id="ajax_loading" align="center"> <img align="absmiddle" src="spinner.gif"> Processing... </div>
                   <div id="login_response" style="color:red; font-weight:bold"> </div>
                   <div id="caps_lock" style="display:none; color:red; font-weight:bold">Caps Lock taustiņš ir ieslēgts.<br />
                     Pārliecinies, ka raksti paroli pareizi!</div>
                 </center>
               </li>
             </ul>
           </form>

js

		$(document).ready(function() { 
 $("#login_form").submit(function(){ 
	$("#submit_login").hide();
	$("#ajax_loading").show();
	var str = $(this).serialize();  

    $.post("login.php", str, function(msg){
	  $("#login_response").ajaxComplete(function(event, request, settings){ 
		$("#submit_login").show();
		$("#ajax_loading").hide(); 
		if(msg == "OK") {
		 loadContents("#outer_top_right", "your_account.php");
		}
		else {
		 var login_response = msg;
		 $("#login_response").html(login_response);
		}  
      });  
    }); 

 return false;  
 });  
});
function loadContents(div, url) {
	$(div).load(url);
}

php

<?PHP
include("connect.php");

 $username = mysql_real_escape_string($_POST['username']);
 $password = mysql_real_escape_string($_POST['password']);

 $sql = mysql_query("SELECT `id` FROM `account` WHERE `login_name`='$username' AND `password`='$password'", $db);
 $result = mysql_num_rows($sql);

 if($result!=1) {
  session_start();
  $_SESSION['mauka'] = $username;
  echo "Username or/and password is wrong.";
 }
 else {
  session_start();
  $_SESSION['star'] = $username;
  echo "OK";
 }
?>

your_account.php

<?PHP
session_start();
?>
<div class="dialog">
       <div class="content">
         <div class="t"></div>
         <div class="m_title">Your accounet</div>
         <div class="m_content">
           <b>Logged in as:</b>
         </div>
       </div>
       <div class="b">
         <div></div>
       </div>
     </div>
   </div>

 

Tātad, lietotājs ievada login datus. Kad nosūtīta login forma, tad js izpildās visas tās darbības un padod post uz php. Php pārbauda vai profils ir vai nav (nav svarīgi šobrīd cik droša ir datu pārbaude) un, ja dati ir pareizi, tad nosūta atpakaļ ziņu un vajadzētu izveidot sesiju['star'], kas saturēto session_id(); un ielādēt <div id="outer_top_right"></div> to html failiņu. Bet kaut kādu iemeslu pēc star sesiju neizveido. Kāpēc, kā salabot?

Edited by thesnarkie
Link to post
Share on other sites

php atgriež OK?

 

Kādēļ tu session_id() glabā vēl kādā sesijas mainīgajā? session_id() pats par sevi satur sesijas id (kuru vajadzētu pašam uzģenerēt).

 

Lai nu kā - ja php atgriež OK, tad lai sesija iedarbotos vajadzēs parlādēt lapu.

Link to post
Share on other sites

Ok, updeitoju pirmo rakstu ar šī brīža failiem.

Mēģināju, nekādu sesiju neizveido.. Mēģināju arī izveidot sesiju, pat, ja dati nav pareizi, bet arī tad nekādu sesiju star viņš neizveido.

 

Pamēģiniet paši ielogoties: šeit.

Login: test

Pass: test

Edited by thesnarkie
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...