dougou1943 2010-12-21 08:29
浏览 69
已采纳

关于全局变量和表单请求的PHP问题

This is probably a stupid question but will ask anyway sine I have no idea.

I have written basic php code which serve forms. Say I have a login page and I serve it using the login.php page and it will be called in the login.html page like this -

<form action="login.php" method="post">

By this it is also implied that every POST needs its own php file, doesn't it? This kind of feels weird. Is there a way to have a single file, say code.php, and just have each of the forms as functions instead?

EDIT: Specifically, say I have 5 forms that are used one after the other in my application. Say after login the user does A, B, C and D tasks each of which are sent to the server as a POST request. So instead of having A.php, B.php, C.php and D.php I would like to have a single code.php and have A(), B(), C() and D() as functions. Is there a way to do this?

Also on the same note, how do I deal with say a global array (e.g. an array of currently logged in users) across multiple forms? I want to do this without writing to a DB. I know its probably better to write to a DB and query but is it even possible to do it with a global array? The reason I was thinking about having all the form functions in one file is to use a global array.

Thanks, - Pav

  • 写回答

2条回答 默认 最新

  • dougengqiu8031 2010-12-21 08:46
    关注

    Sure, no problem, just POST all forms to the same .php file and have it deal with all incoming POST data. There's nothing keeping you from doing it.

    Usually your forms send different data though and each form needs a bit of custom handling. If you put all this logic into the same handler file, it'll become a reaaaally long, messy piece of code. It's better to separate code into different files if it does different things.

    You can and should abstract the common parts like database connection handling into functions or objects and include them in each POST handler though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?