简体   繁体   English

session_start()不能在线工作

[英]session_start() doesn't work online

I'm having a problem to set a session for a user. 我在为用户设置会话时遇到问题。 it's working in my localhost but online it doesn't work. 它在我的本地主机上正常工作,但在网上却无法正常工作。

I have a username called 's' and I have created a page to see what is the problem 我有一个名为“ s”的用户名,并且创建了一个页面来查看问题所在

here is the code. 这是代码。 test.php test.php

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

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

?>

<div class="center_body">
    <div class="search_field">
            <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <table border="0" width="100%" cellspacing="0" cellpadding="0">
                    <td align="left">username:</td>
                    <td align="right"><input type="text" name="username" class="search" /></td>
                </tr>

                </table>
                <input type="submit" value="log in" class="search" /></p>
            </form>
            </tr>

    </div>
</div>

<?php
$result = mysql_query("SELECT username FROM users WHERE username='$username'") or die(mysql_error);

    while($row = mysql_fetch_assoc($result))
    {
        $user = $row['username'];

    }

    if($username == $user)
    {
        echo '<div class="search_field"><b>you have logged successfully</b></div></ br>';
       $_SESSION['username'] = $username;

    }

    if(isset($_SESSION['username']))
    {
        echo $_SESSION['username'];
    }

?>  

like above code, it works, and also I can print the username. 像上面的代码一样,它可以工作,而且我也可以打印用户名。 but when I edit the test.php to be 但是当我编辑test.php是

<?php 
session_start();
require 'connect.php';
    if(isset($_SESSION['username']))
    {
        echo $_SESSION['username'];
    }

?>  

now, this page should print username because I have successfully created the session. 现在,此页面应打印用户名,因为我已经成功创建了会话。 but it prints nothing so session seems not working 但它没有打印任何内容,因此会话似乎无法正常工作

what do you thing is the problem? 你是怎么回事?

EDIT: connect.php 编辑:connect.php

<?php

$connect = @mysql_connect("localhost","root","") or die("error in connection");
$select  = @mysql_select_db("abc") or die ("no databse selected");

?>

of course I changed data to be connected to my host databas 当然,我更改了数据以将其连接到主机数据库

try it: 试试吧:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $result = mysql_query("SELECT username FROM users WHERE username='$username' LIMIT 1") or die(mysql_error);
    while($row = mysql_fetch_assoc($result))
    {

        if($username == $row['username'])
        {
            echo '<div class="search_field"><b>you have logged successfully</b></div></ br>';
           $_SESSION['username'] = $username;
           break;

        }

    }    
}

ok guys as I thought it's from my host site. 好的,因为我以为是我的托管网站提供的。

the support tell me "I have set session.save_path for your account." 支持人员告诉我“我已经为您的帐户设置了session.save_path”。 and the problem is solved. 问题就解决了。

big thanks to you guys and especially for Danial :) 非常感谢你们,尤其是Danial :)

all the best to you :) 祝你好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM