跨越跨域网页的PHP会话传播
此登录表单旨在使用会话。它工作正常但会话不会传播 跨域的其他页面。似乎没有设置会话。任何帮助谢谢
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#username').focus(); // Focus to the username field on body loads
$('#login').click(function(){ // Create `click` event function for login
var username = $('#username'); // Get the username field
var password = $('#password'); // Get the password field
var login_result = $('.login_result'); // Get the login result div
login_result.html('loading..'); // Set the pre-loader can be an animation
if(username.val() == ''){ // Check the username values is empty or not
username.focus(); // focus to the filed
login_result.html('<span class="error">Enter the username</span>');
return false;
}
if(password.val() == ''){ // Check the password values is empty or not
password.focus();
login_result.html('<span class="error">Enter the password</span>');
return false;
}
if(username.val() != '' && password.val() != ''){ // Check the username and password values is not empty and make the ajax request
var UrlToPass = 'action=login&username='+username.val()+'&password='+password.val();
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type : 'POST',
data : UrlToPass,
url : 'http://crossdomain.com/ajax.php',
success: function(responseText){ // Get the result and asign to each cases
if(responseText == 0){
login_result.html('<span class="error">Username or Password Incorrect!</span>');
}
else if(responseText == 1){
window.location = 'userpage.php';
}
else{
alert('Problem with sql query');
}
}
});
}
return false;
});
});
</script>
<?php
session_start();
include 'db.php';
if(isset($_POST['action']) && $_POST['action'] == 'login'){
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$query = mysql_query('SELECT * FROM users WHERE username = "'.$username.'" AND password = "'.$password.'" ');
$num_rows = mysql_num_rows($query); // Get the number of rows
if($num_rows <= 0){
echo 0;
} else {
$fetch = mysql_fetch_array($query);
$_SESSION['userid'] = $fetch['id'];
$_SESSION['username'] = $fetch['username'];
echo 1;
}
}
?>