PHP的MySQL +会话的问题 [英] php mysql + session problems

查看:79
本文介绍了PHP的MySQL +会话的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php和mysql创建一个简单的登录和注销脚本,但是当我尝试输入login.php或索引文件时,出现一条错误消息: **页面无法正确重定向 Firefox检测到服务器正在以一种永远无法完成的方式重定向对该地址的请求.

i am creating a simple login and logout script using php and mysql but when i try to enter the login.php or the index file i get an error message that say : **The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

有时可能是由于禁用或拒绝接受而导致此问题 饼干.** 我不知道如何解决,或者如果有人帮助我会出现什么错误,我将不胜感激

This problem can sometimes be caused by disabling or refusing to accept cookies.** i do not know how to solve or what is the error if anyone help me i will be appreciate

<?php
require_once('connect.php');
ob_start();
session_start();
//checked wether the user is loged in  or not 

$user = $_SESSION['username'];

if(!isset($_SESSION['username']))
{
    $user = $_SESSION['username'];
    header("Location: index.php");
    exit();
}
else
{

    header("Location: home.php");
}





// login script
if(isset($_POST['username'])&& isset($_POST['password']))
{
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']);
    $user_password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password']);
    $md5password = md5($user_password);
    $sql = mysql_query("SELECT id FROM members WHERE username = '".$user_login."' AND password = '".$user_password."'") or die ("could not select from database");

    $userCount = mysql_num_rows($sql);
    if($userCount ==1)
    {
        while($row = mysql_fetch_array($sql))
        {

            $id = $row['id'];
        }

        $_SESSION['id'] = $id;
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: index.php");
        exit();
    }
    else
    {
         echo "that info is incorrect";
         exit();
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="login.php" method="post">

<input name="username" type="text" value="username" size="32" />
<input name="pass" type="password" value="password" size="32" />
<input name="login" type="submit" value="login" />

</form>

</body>
</html>
<?php  ob_end_flush(); ?>

home.php

<?php
//home.php
session_start();
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{

    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";


}


?>

logout.php

<?php
session_start();
session_destroy();
header("Location: index.php");

?>

推荐答案

首先,在index.php中,您不需要"//检查用户是否已登录",我们应该在家中进行检查.的PHP. 这段代码导致您出现错误:页面无法正确重定向,Firefox已检测到服务器正在以永远无法完成的方式重定向对该地址的请求".您进行了重复(未创建会话,但已选中...).

First, in index.php you don't need to "//checked wether the user is loged in or not", we should check that in home.php. This code is causing your error : "The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete". You made a repetition (The session is not created but it is checked ...).

第二,在home.php中,您必须编写session_start()方法,这是使用session时需要的代码.

Second, in home.php, You have to write session_start() method, this is the code require when using session.

请参阅我的代码

index.php

<?php
ob_start();
session_start();

//check session is existed    
if (isset($_SESSION['username'])) {
    header("Location: home.php");
}

if (isset($_POST['username']) && isset($_POST['password'])) {
    $user_login = $_POST['username'];
    $user_password = $_POST['password'];

    if ($user_login == 'namluu' && $user_password =='123456') {
        $_SESSION['username'] = $user_login;
        $_SESSION['password'] = $user_password;
        header("Location: home.php");
        exit();
    } else {
        echo 'Infor not correct';
        exit();
    }
}
?>
<html>
  <head></head>
  <body>
    <form action="index.php" method="post">
      <input type="text" name="username" />
      <input type="text" name="password" />
      <input type="submit" name="login" value="login" />
    </form>
  </body>
</html>
<?php
  ob_end_flush();
?>

home.php

<?php
session_start();
//home.php
$user = $_SESSION['username'];
if(!isset($_SESSION['username']))
{
    header("Location: index.php");
    exit();
}
else
{
    echo "hi $user you are loged in //Welcome to our website <a href=\"logout.php\">Logout</a>";
}
?>

这篇关于PHP的MySQL +会话的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆