首次尝试在实时服务器上无法登录 [英] Login doesn't work in 1st attempt on live server

查看:54
本文介绍了首次尝试在实时服务器上无法登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,我做了一个简单的登录页面,它在本地服务器上可以正常工作,但在实时服务器上第一次尝试时不起作用.第一次尝试后,然后可以工作.我认为问题出在会议上,但我无法找出确切的问题.再次详细描述(在任何浏览器中,第一次尝试后,它都会显示相同的登录页面,而不是在main.php上重定向.然后,在第二次尝试后,它将在main.php页面上重定向,并且可以正常工作,直到浏览器运行为止.第二天再次需要如果错误的凭据从第一次尝试起就可以进行2次尝试,但是如果凭据正确,则需要进行2次尝试.)

I am new in PHP and I made a simple login page.It works fine on local server but doesn't works on first attempt on live server. After the 1st attempt then does works. I think the problem is in sessions, but I unable to figure out the exact problem. Once again describe in detail (In any Browser after 1st attempt it shows same login page instead of redirect on main.php. then after 2nd attempt it redirects on main.php page and works fine until browser has been running. Next day again it needs 2 attempts when wrong credentials works fine from the 1st attempt but with correct credentials it requires 2 attempt.)

**login.php**
<form action="login.php" method="post" onsubmit="return loginvalidate()" name="loginfrm">
<table width="600" border="0">
  <tr>
    <td>User Name</td>
    <td><input name="txtusername" type="text" maxlength="15" id="txtusername"/></td> 
    <span id="message"></span>
  </tr>
  <tr>
    <td></td>
    <td><span style="color:#FF0000">*</span> Minimum 5 alphanumeric letters (a-z A-Z 0-9)</td></tr>
  <tr>

    <td>Password</td>

    <td><input name="txtpass" type="password" maxlength="7" />

    <span style="color:#FF0000">*</span> Minimum 5 alphanumeric letters (a-z A-Z 0-9)</td>

  </tr>

  <tr>

    <td>&nbsp;</td>

    <td><input type="submit" name="submit"/></td>

  </tr>

   <tr>

    <td><a href="">Forget password?</a></td>

    <td><a href="registration.php">New Register</a></td>

  </tr>

  </table>

</form>



<?PHP

session_start();
function login()
{
include ("includes/dbConfig.php");
$uname = $_POST["txtusername"];
$pass = $_POST["txtpass"];
$uname = stripslashes($uname); 
$pass =  stripslashes($pass);
$uname = mysql_real_escape_string($uname);
$pass = mysql_real_escape_string($pass);
$log = mysql_query("SELECT * FROM logininfo WHERE username = '$uname' and password = '$pass'");
if(mysql_num_rows($log)==1)
{
  $_SESSION["username"] = $uname;
  //header("location: main.php");
  echo ("<script type='text/javascript'>window.location.href='main.php'</script>");                 
}
else
{
  echo "<script type='text/javascript'>alert('Login Fail - Check Username and password')</script>";
}
mysql_close($ms);
}

if(isset($_POST['submit']))
{
   login();
} 
?>

main.php

<?php
session_start();
$temp = $_SESSION["username"];
echo "$temp";
if (!isset($_SESSION["username"]))
{
header('Location: admin.php');
}
?>

推荐答案

您放置了session_start();位于页面顶部,但没有登录功能.请使用此代码.

You put session_start(); at top of page but not in login function.use this code.

 **login.php**  

<?PHP
function login()
{
include ("includes/dbConfig.php");
$uname = $_POST["txtusername"];
$pass = $_POST["txtpass"];
$uname = stripslashes($uname); 
$pass =  stripslashes($pass);
$uname = mysql_real_escape_string($uname);
$pass = mysql_real_escape_string($pass);
$log = mysql_query("SELECT * FROM logininfo WHERE username = '$uname' and password = '$pass'");
    if(mysql_num_rows($log)==1)
    {
    session_start();
    $_SESSION["username"] = $uname;
        //header("location: main.php");
    echo ("<script type='text/javascript'>window.location.href='main.php'</script>");               
}
else
{
    echo "<script type='text/javascript'>alert('Login Fail - Check Username and password')</script>";

    }
mysql_close($ms);
}

  if(isset($_POST['submit']))
 {
    login();
 } 
?>

这篇关于首次尝试在实时服务器上无法登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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