douhong6187 2017-04-08 18:57
浏览 10

从不同的文件中获取会话数组

I can't access array from other file and still don't figure it out, whether my data already stored into array or not. Should i put $_SESSION into the function?

Lat3_3a.php

<form id="form1" name="form1" method="post" action="Lat3_3b.php">
    Insert number: <input type="number" name="num" id="num" />
    <input type="submit" name="button" id="button" value="OK" />
</form>

Lat3_3b.php

<?php
   session_start();
    $_SESSION["num"] = $_POST["num"];
    if (empty($_SESSION["num"]))
        echo "Please, insert number";
    else {
        $val=$_POST['num'];
    echo " Factorial  " .$val. " ! =  " .factorial($val)."<br/>";
    echo "<a href='Lat3_3c.php'>Link</a>";
  }

    function factorial($val){

         if($val<=1){
            $result=1;
            return $result; 
         }elseif($val>1){
            for($i=1; $i<=$val; $i++){
               $result=$val * factorial($val-1);
         }
         return $result;
         }

         $data=array($val,$result,"12345", "Travis");
         $_SESSION["var"]=$_POST["data"];    
  }
  ?>

Lat3_3c.php

<?php
  session_start();
  if(empty($_SESSION["var"]))
      echo "Variable not found";
  else
  echo "Data : ". $_SESSION["var"];

?>
  • 写回答

1条回答 默认 最新

  • dongtiao5094 2017-04-08 20:55
    关注

    Ok looking a bit more in to your code, you need to learn more about the session, post and functions. For that a look at the PHP manual site. Site http://php.net

    For your code to work you need to use something like this:

    <?php
       session_start();
        $_SESSION["num"] = $_POST["num"];
        $_SESSION["var"]=array();
        if (empty($_SESSION["num"])){
            echo "Please, insert number";
        }
        else {
            $val=$_POST['num'];
            $function_result=factorial($val);
            $data=array($val,$function_result,"12345","Travis");
            $_SESSION["var"]=$data;
            echo " Factorial  " .$val. " ! =  " .$function_result."<br/>";
            echo "<a href='Lat3_3c.php'>Link</a>";
        }
        function factorial($val){
    
             if($val<=1){
                $result=1;
                return $result; 
             }elseif($val>1){
                for($i=1; $i<=$val; $i++){
                   $result=$val * factorial($val-1);
                }
                return $result;
             }
         }
      ?>
    

    I'm not looking into your math.

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序