PHP-会话在移动设备上不起作用

PHP-会话在移动设备上不起作用,php,session,session-variables,Php,Session,Session Variables,对于使用会话\u start()登录im,对于桌面,它可以正常工作,但对于移动设备,它不工作。当我使用变量创建一个session\u id()时,例如,session\u id('login')它在移动设备上工作,但会中断其他计算机上的其他会话。但是当自动生成session\u id()时,它在移动设备上不起作用。我该怎么办 <?php session_start(); include 'connect.php'; if(isset($_POST['submit'])) {

对于使用
会话\u start()
登录im,对于桌面,它可以正常工作,但对于移动设备,它不工作。当我使用变量创建一个
session\u id()
时,例如,
session\u id('login')
它在移动设备上工作,但会中断其他计算机上的其他会话。但是当自动生成
session\u id()
时,它在移动设备上不起作用。我该怎么办

<?php
session_start();
include 'connect.php'; 

if(isset($_POST['submit']))
{
  $username = htmlspecialchars($_POST["uname"], ENT_QUOTES, "ISO-8859-1");
  $username = stripslashes($username);
  $password = htmlspecialchars($_POST["pass"], ENT_QUOTES, "ISO-8859-1");
  $password = stripslashes($password);

  $sql="SELECT * FROM member WHERE username= '$username' AND password='$password' AND status='1' ";
  $result=mysql_db_query($dbname,$sql);
  $row=mysql_fetch_row($result);
  $num=mysql_num_rows($result);
  $msg= "<h3 style='padding:0px;margin:0px;font-size:16px;background:#fff;padding:8px;'><font color='red'><center>Incorrect login data</center></font></h3>";

  if($num>0)
  {
    $_SESSION['id']       =$row[0];
    $_SESSION['fullName'] =$row[1];
    $_SESSION['username'] =$row[2];
    $_SESSION['password'] =$row[3];
    $_SESSION['email']    =$row[4];
    $_SESSION['userType'] =$row[5];
    $_SESSION['status']   =$row[6];
    header('location:index.php');
  }
}
?>
我的会话\u index.php上的启动代码

session_start();
if (isset($_SESSION['username'])){
    session_id();
}
login.php文件的完整代码

<?php
require('config.php');
$usernameOK = false; $passwordOK = false;
if (isset($_POST['username']) and isset($_POST['password'])){

        $username = $_POST['username'];
        $password = $_POST['password'];

        $UserQuery = "SELECT username FROM `members` WHERE username='$username'";
        $userTestResult = mysqli_query($connection, $UserQuery);
        $usernameTEST = mysqli_num_rows($userTestResult);
        if($usernameTEST == 1){$usernameOK = true;}

        $PasswordQuery = "SELECT password FROM `members` WHERE username='$username'";
        $result = mysqli_query($connection,$PasswordQuery);
        $row = mysqli_fetch_row($result);

        if (password_verify($password, $row[0])){$passwordOK = true;}

        if($usernameOK == true && $passwordOK == true){
            $_SESSION['username'] = $username;
        }else{
            $error_message = "Incorrect login data";
        }
    }

    if (isset($_SESSION['username'])){
        header("Location: ../");
        exit;
    }else{?>
    <form class="login-form" method="POST">
        <?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>

<?php } ?>
<?php
require('config.php');
$usernameOK = false; $passwordOK = false;
if (isset($_POST['username']) and isset($_POST['password'])){

        $username = $_POST['username'];
        $password = $_POST['password'];

        $UserQuery = "SELECT username FROM `members` WHERE username='$username'";
        $userTestResult = mysqli_query($connection, $UserQuery);
        $usernameTEST = mysqli_num_rows($userTestResult);
        if($usernameTEST == 1){$usernameOK = true;}

        $PasswordQuery = "SELECT password FROM `members` WHERE username='$username'";
        $result = mysqli_query($connection,$PasswordQuery);
        $row = mysqli_fetch_row($result);

        if($usernameOK == true && password_verify($password, $row[0])){
            if (isset($_SESSION)){
               $_SESSION['username'] = $username;
            } else{
               session_start();
               $_SESSION['username'] = $username;
            }
        }else{
            $error_message = "Incorrect login data";
        }
    }

    if (isset($_SESSION['username'])){
        header("Location: ../");
        exit;
    }else{?>
    <form class="login-form" method="POST">
        <?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>

<?php } ?>

用户名
密码
登录

我不太清楚你的代码是做什么的,因为我看不到任何关于设置用户名或诸如此类的内容。代码的作用是启动会话并检查会话中的username变量是否已设置,如果已设置,则调用
session\u id

你能用它来检查会话创建是否正确吗

session_start();
if (session_status() !== PHP_SESSION_NONE)
{
echo session_id();
}

如果会话状态不是
PHP\u session\u NONE

,则启动会话并显示会话ID。您的会话可能未设置。根据您使用的PHP版本,检查会话

对于PHP的版本=5.4.0

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
对于PHP的版本<5.4.0

if(session_id() == '') {
    session_start();
}

启动会话不需要执行session\u id(),session\u start()就足够了

我在iPhone 6上的Safari上测试了以下内容(如果需要,只需重新添加db查询部分):

testa.php

<?php
//require('config.php');

session_start();
echo "Session ID: ".session_id()." <br>\n";

$usernameOK = false;
$passwordOK = false;

if (isset($_POST['username']) and isset($_POST['password'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];

    $usernameOK = true;
    $passwordOK = true;

    if ($usernameOK == true && $passwordOK == true) {
        $_SESSION['username'] = $username;
    } else {
        $error_message = "Incorrect login data";
    }
}

if (isset($_SESSION['username'])) {
    header("Location: ../");
    exit;
} else {
?>
    <form class="login-form" method="POST">
        <?php if (isset($error_message)) { ?>
            <div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>
<?php } ?>
<?php

session_start();
echo "Session ID: ".session_id()." <br>\n";
echo "Data stored in session: {$_SESSION['username']}<br>\n";
 <?php
    $username = "deadlock";
    if (isset($_SESSION))
    {
        $_SESSION['username'] = $username;
    }
    else
    {
        session_start();
        $_SESSION['username'] = $username;
    }
     print_r($_SESSION);
<?php
if (isset($_SESSION['username'])){
    session_regenerate_id(true);
} else if (!isset($_SESSION)){
    session_start();
}
print_r(session_id());

用户名
密码
登录
testb.php

<?php
//require('config.php');

session_start();
echo "Session ID: ".session_id()." <br>\n";

$usernameOK = false;
$passwordOK = false;

if (isset($_POST['username']) and isset($_POST['password'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];

    $usernameOK = true;
    $passwordOK = true;

    if ($usernameOK == true && $passwordOK == true) {
        $_SESSION['username'] = $username;
    } else {
        $error_message = "Incorrect login data";
    }
}

if (isset($_SESSION['username'])) {
    header("Location: ../");
    exit;
} else {
?>
    <form class="login-form" method="POST">
        <?php if (isset($error_message)) { ?>
            <div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>
<?php } ?>
<?php

session_start();
echo "Session ID: ".session_id()." <br>\n";
echo "Data stored in session: {$_SESSION['username']}<br>\n";
 <?php
    $username = "deadlock";
    if (isset($_SESSION))
    {
        $_SESSION['username'] = $username;
    }
    else
    {
        session_start();
        $_SESSION['username'] = $username;
    }
     print_r($_SESSION);
<?php
if (isset($_SESSION['username'])){
    session_regenerate_id(true);
} else if (!isset($_SESSION)){
    session_start();
}
print_r(session_id());

肯定可以在移动浏览器上使用

我简化了代码,当所有检查都通过时,会话将启动

<?php
require('config.php');
if (isset($_POST['username']) and isset($_POST['password'])){

    $username = $_POST['username'];
    $password = $_POST['password'];

    $UserQuery = "SELECT password FROM `members` WHERE username='$username'";
    $result = mysqli_query($connection, $UserQuery);
    $usernameTEST = mysqli_num_rows($result);
    if($usernameTEST == 1){
        $row = mysqli_fetch_row($result);
        if(password_verify($password, $row[0])) {
            session_start();
            $_SESSION['username'] = $username;
            header("Location: ../");
            exit();
        }
    }

    $error_message = "Incorrect login data";
}

<form class="login-form" method="POST">
    <?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

    <span class="placeholder">username</span>
    <input type="text" name="username" placeholder="username" required>

    <span class="placeholder">password</span>
    <input type="password" name="password" id="inputPassword" placeholder="password" required>

    <button type="submit">Login</button>
    <a class="form-link" href="register.php">Register</a>
</form>

用户名
密码
登录

可能您遇到了一些冲突问题:

我的会话\u开始代码

if (isset($_SESSION['username'])){
    session_id();
} else if (!isset($_SESSION)){
   session_start();
}
login.php文件的完整代码

<?php
require('config.php');
$usernameOK = false; $passwordOK = false;
if (isset($_POST['username']) and isset($_POST['password'])){

        $username = $_POST['username'];
        $password = $_POST['password'];

        $UserQuery = "SELECT username FROM `members` WHERE username='$username'";
        $userTestResult = mysqli_query($connection, $UserQuery);
        $usernameTEST = mysqli_num_rows($userTestResult);
        if($usernameTEST == 1){$usernameOK = true;}

        $PasswordQuery = "SELECT password FROM `members` WHERE username='$username'";
        $result = mysqli_query($connection,$PasswordQuery);
        $row = mysqli_fetch_row($result);

        if (password_verify($password, $row[0])){$passwordOK = true;}

        if($usernameOK == true && $passwordOK == true){
            $_SESSION['username'] = $username;
        }else{
            $error_message = "Incorrect login data";
        }
    }

    if (isset($_SESSION['username'])){
        header("Location: ../");
        exit;
    }else{?>
    <form class="login-form" method="POST">
        <?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>

<?php } ?>
<?php
require('config.php');
$usernameOK = false; $passwordOK = false;
if (isset($_POST['username']) and isset($_POST['password'])){

        $username = $_POST['username'];
        $password = $_POST['password'];

        $UserQuery = "SELECT username FROM `members` WHERE username='$username'";
        $userTestResult = mysqli_query($connection, $UserQuery);
        $usernameTEST = mysqli_num_rows($userTestResult);
        if($usernameTEST == 1){$usernameOK = true;}

        $PasswordQuery = "SELECT password FROM `members` WHERE username='$username'";
        $result = mysqli_query($connection,$PasswordQuery);
        $row = mysqli_fetch_row($result);

        if($usernameOK == true && password_verify($password, $row[0])){
            if (isset($_SESSION)){
               $_SESSION['username'] = $username;
            } else{
               session_start();
               $_SESSION['username'] = $username;
            }
        }else{
            $error_message = "Incorrect login data";
        }
    }

    if (isset($_SESSION['username'])){
        header("Location: ../");
        exit;
    }else{?>
    <form class="login-form" method="POST">
        <?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>

<?php } ?>
编辑:我注意到您正在使用函数重定向到索引页。除非您将用户发送出系统,否则避免此类重定向非常重要,因为某些环境不能很好地使用它们。我建议将例程改为用户POST/GET http例程:

内部validateSession.php

在validateSession之外,与会话规则无关,将所有内容集中在那里

index.php/head.php/一些总是在每个页面中开始的代码只会开始:

<?php 
include_once('validateSessions.php'); 
?>

您的表格:

<form class="login-form" action="index.php" method="POST"> 
<?php if(isset($error_message)){ ?><div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?> 

<span class="placeholder">username</span> 
<input type="text" name="username" placeholder="username" required> 

<span class="placeholder">password</span> 
<input type="password" name="password" id="inputPassword" placeholder="password" required> 

<button type="submit">Login</button> 
<a class="form-link" href="register.php">Register</a> 
</form>

用户名
密码
登录
编辑:我回顾了你的例程,注意validateSessions.php,我忘记在调用该文件的每个例程中加载session_start()。将validateSessions放在include目录中


更新:即使我的代码帮助了OP,主要的问题还是将会话配置到带有SESSION.save_路径的php.ini文件中,导致会话例程出现问题。php.ini修复程序解决了这个问题,我认为这段代码有助于OP思考一些有关会话内容的例程。

简化了代码,使其更易于理解。忽略用户名和密码等检查。您基本上是在尝试设置会话变量。这在login.php中发生。代码是

 <?php
 $username = "Umesh";
 if (isset($_SESSION))
 {
      $_SESSION['username'] = $username;
 }
 else
 {
           session_start();
           $_SESSION['username'] = $username;
 }
 print_r($_SESSION);
?>
以上内容适用于我的所有浏览器和设备。我甚至在iphone上测试了chrome。它似乎工作得很好。请检查一下,看它是否有效。如果没有,我们将不得不检查您的Web服务器,看看该系统上的会话对象有什么问题


我验证了print\u r($\u SESSION)在这两种情况下是相同的。

根据您的上一条评论,我猜这是由于您使用的查询字符串。基本上,使用
password
作为列名有时会中断查询,因为
password
也是一个mysql关键字。 此外,关于您的会话,您可以参考以了解有关功能的更多详细信息
Session\u id(“login”)
基本上将会话id设置为login,该id在您的移动设备上工作,因为这将创建具有特定静态id的会话。但是,您可以尝试使用
Session\u regenerate\u id(true)
查看是否有帮助

我还尝试通过将静态值作为用户名来执行代码,如下所示

login.php

<?php
//require('config.php');

session_start();
echo "Session ID: ".session_id()." <br>\n";

$usernameOK = false;
$passwordOK = false;

if (isset($_POST['username']) and isset($_POST['password'])) {

    $username = $_POST['username'];
    $password = $_POST['password'];

    $usernameOK = true;
    $passwordOK = true;

    if ($usernameOK == true && $passwordOK == true) {
        $_SESSION['username'] = $username;
    } else {
        $error_message = "Incorrect login data";
    }
}

if (isset($_SESSION['username'])) {
    header("Location: ../");
    exit;
} else {
?>
    <form class="login-form" method="POST">
        <?php if (isset($error_message)) { ?>
            <div class="mini-mes error"> <?php echo $error_message; ?> </div><?php } ?>

        <span class="placeholder">username</span>
        <input type="text" name="username" placeholder="username" required>

        <span class="placeholder">password</span>
        <input type="password" name="password" id="inputPassword" placeholder="password" required>

        <button type="submit">Login</button>
        <a class="form-link" href="register.php">Register</a>
    </form>
<?php } ?>
<?php

session_start();
echo "Session ID: ".session_id()." <br>\n";
echo "Data stored in session: {$_SESSION['username']}<br>\n";
 <?php
    $username = "deadlock";
    if (isset($_SESSION))
    {
        $_SESSION['username'] = $username;
    }
    else
    {
        session_start();
        $_SESSION['username'] = $username;
    }
     print_r($_SESSION);
<?php
if (isset($_SESSION['username'])){
    session_regenerate_id(true);
} else if (!isset($_SESSION)){
    session_start();
}
print_r(session_id());
为什么要使用session\u id()。会话我总是使用这个登录系统。我的桌面或移动会话没有问题

PHP代码

?php
会话_start();
包括“connect.php”;
如果(isset($_POST['submit']))
{
$username=htmlspecialchars($_POST[“uname”],ENT_引号,“ISO-8859-1”);
$username=stripslashes($username);
$password=htmlspecialchars($_POST[“pass”],ENT_引号,“ISO-8859-1”);
$password=stripslashes($password);
$sql=“从用户名为“$username”和密码为“$password”且状态为“1”的成员中选择*;
$result=mysql\u db\u查询($dbname,$sql);
$row=mysql\u fetch\u row($result);
$num=mysql\u num\u行($result);
$msg=“不正确的登录数据”;
如果($num>0)
{
$\会话['id']=$行[0];
$\会话['fullName]=$row[1];
$\会话['username']=$row[2];
$\会话['password']=$row[3];
$\会话['email']=$row[4];
$\会话['userType']=$row[5];
$\会话['status']=$行[6];
标题('location:index.php');
}
}
?>

我的HTML表单

if (session_status() == PHP_SESSION_NONE) 
{
    session_start();
}

忘记了吗

登录 删除会话_start();在index.php中 并将其添加到config.php中


我的问题出现在服务器端的
会话上。保存路径未定义到我的服务器。非常感谢所有帮助我的人:)

检查会话\u start()是否放在任何浏览器输出之前。我发现桌面上的Chrome浏览器比其他浏览器以及移动Chrome浏览器要宽容得多

这个问题是由Chrome“Lite模式”产生的,以减少手机的数据负载。如果你使用秘密导航,那么li