会话/非会话自动授权

时间:2013-05-22 16:39:46

标签: php

你好,我在使用flashcoms进行自动登录时遇到问题视频聊天在这里是我的登录脚本

users.php

    <?php
    function logged_in () {
        return (isset($_SESSION['user_id'])) ? true : false;
    }

    function user_exists($username) {
        $username = sanitize($username);
        return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;   
    }

    function user_id_from_username ($username) {
        $username = sanitize($username);
        return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
    }

    function login($username, $password) {
        $user_id = user_id_from_username($username);

        $username = sanitize ($username);
        $password = md5($password);

        $query = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE username = '$username' AND password = '$password'");
        return (mysql_result($query, 0) == 1) ? $user_id : false;
    }
?>

的login.php

<?php
include 'core/init.php';


if (empty($_POST) === false) {
    $username = $_POST ['username'];
    $password = $_POST ['password'];

    if (empty($username) === true || empty ($password) === true)    {
        $errors [] = 'you need to enter username and password';

    } else if (user_exists($username) === false)   {
        $errors [] = 'we can\'t find that username. have you registred?';
    } else {
        $login = login($username, $password);
        if ($login === false)  {
            $errors [] = 'That username or password is inccorect.';

        } else {
            $_SESSION['user_id'] = $login;
            header('Location: logged_in.php');
            exit();

        }

    }

    print_r($errors);
}
?>

这里是视频聊天的handlers.php

<?php
require_once '../../../common/server/php/settings.php';

//Connect to users database
$db = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('test',$db) or die(mysql_error());

//Init request parameters
$userName = (isset($_REQUEST["user_name"])) ? urldecode($_REQUEST["user_name"]) : "";
$password = (isset($_REQUEST["password"])) ? urldecode($_REQUEST["password"]) : "";
$uid = (isset($_REQUEST["uid"])) ? urldecode($_REQUEST["uid"]) : "";
$password = md5($password);

//Check if user filled login and password in the login screen (Chat authorization)
if($userName != "" && $password != "")
{
  $sql = "SELECT * FROM users WHERE username='".$userName."' AND password='".$password."'";
}
//session/cookie base authorization (Auto login)
else if ($_SESSION['user_id']!="")
{
  $sql = "SELECT * FROM users WHERE user_id='".$_SESSION["user_id"]."'";
}
// Non session/cookie based autologin authorization
else if ($uid!="")
{
  $sql = "SELECT * FROM users WHERE id='".$_GET['uid']."'";
}
else
{
  echo '<auth error="AUTH_ERROR" />';
  exit;
}

//Select user data
$result = mysql_query($sql,$db);

if(mysql_num_rows($result)==1)
{
  //User found. get user info
  $usersInfo = mysql_fetch_array($result);

  $photo = FLASHCOMS_HTTP_ROOT.'common/images/User1_120.png';
  $photoModeImage = FLASHCOMS_HTTP_ROOT.'common/images/User1_40.png';   

  $answer = '<auth>';
  $answer .= '<userName><![CDATA['.$userName.']]></userName>';
  $answer .= '<gender>male</gender>';
  $answer .= '<age>'.$userInfo['age'].'</age>';
  $answer .= '<level>regular</level>';
  $answer .= '<photo><![CDATA['.$photo.']]></photo>';
  $answer .= '<photoModeImage><![CDATA['.$photoModeImage.']]></photoModeImage>';
  $answer .= '</auth>';
  echo $answer;
  exit;
}
else 
{
  //User not found OR authorization failed
  echo '<auth error="AUTH_ERROR" />';
  exit;
}

?>

这里是init.php

<?php
session_start();
//error_reporting(0);

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

$errors = array();
?>

问题是,如果我在视频聊天的管理面板上设置自动登录它将无法加载视频聊天仍然说验证但我已经登录我的网站然后我传递给视频聊天但仍然无法工作我觉得有什么会话和cookie的问题。

0 个答案:

没有答案