dongzao3923 2014-10-27 14:51
浏览 42

count($ _ SESSION)始终返回0

I wrote this script:

<html>
<head>
<link rel="stylesheet" type="text/css" href="../../css/stile.css"> 
</head>
<body>

<?php
include_once("../../classi/funzioni.php");
include_once("../../classi/config.php");
include_once("../../classi/auth.lib.php");

session_start(); //Inizio la sessione

list($status, $user) = auth_get_status();

if($status == AUTH_LOGGED && $user['nome_negozio']=="Black Fashion"){

echo "<h1>ORDINE DA CLIENTE</h1>";

$prodotto="SELECT * FROM elenco_movimenti WHERE cliente='".$user['nome_negozio']."'";//query
echo "<br><b>QUERY</b><br>";    
echo $prodotto; 

$prodotto=$db_magazzino->query($prodotto);
$prodotto=$prodotto->fetch_row(); 

$i=count($_SESSION);    
$_SESSION[$i]=$prodotto; 
}

?>

<form name="form1" method="post" action="./ordine_da_cliente.php">
<label for="barcode"><b>Codice a barre</b></label>
<input type="text" name="barcode" >
<input type="submit" name="submit" value="OK">
</form>

</body>
</html> 

NB

$i=count($_SESSION); $_SESSION[$i]=$prodotto;

Then if the session has 0 elements, i set $_SESSION[0] width the product, and i see it with var_dump:

VAR DUMP SESSION array(1) { [0]=> array(14) { [0]=> string(1) "0" [1]=> string(19) "2014-10-12 08:31:18" [2]=> string(18) "Carico da articoli" [3]=> string(13) "0913VLA002001" ETC...

but the next count is always zero and i see the same vardump...

Why don't i see $_SESSION[0][0][1][2]. $_SESSION[1][0][1][2], etcetera?

  • 写回答

2条回答 默认 最新

  • douhuan1257 2014-10-27 15:37
    关注

    Your problem is this:

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="../../css/stile.css"> 
    </head>
    <body>
    
    <?php
    include_once("../../classi/funzioni.php");
    include_once("../../classi/config.php");
    include_once("../../classi/auth.lib.php");
    
    session_start(); //Inizio la sessione
    

    By sending everything between <html> and <?php, you have already sent headers to the browser and thus the browser will not have a session variable stored between pages. So PHP will have an empty $_SESSION every time for each user between each page load since the browser hasn't stored the session ID from your server, but will still store $_SESSION variables within the immediate script execution as if you were using just any other variable.

    Change your code to use session_start before any browser output.

    <?php
    session_start(); //Inizio la sessione
    
    include_once("../../classi/funzioni.php");
    include_once("../../classi/config.php");
    include_once("../../classi/auth.lib.php");
    
    ?><html>
    <head>
    <link rel="stylesheet" type="text/css" href="../../css/stile.css"> 
    </head>
    <body>
    <?php
        // .. Rest of your code
    

    And just so you know, if you read the docs fully, it states:

    Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?