简体   繁体   English

将ID从一个php文件传输到另一个

[英]Transfer a id from one php file to another

I'm working on a page where the User can enter an ID which then gets passed to a Controller. 我正在一个页面上,用户可以在其中输入一个ID,然后将该ID传递给Controller。 This Controller handles the ID and generates a uniqID. 该控制器处理ID并生成uniqID。 With this uniqID a new folder gets generated on the server. 使用此uniqID,将在服务器上生成一个新文件夹。

处理

Now there's another PHP File which needs to access the generated Folder on the Server. 现在,还有另一个PHP文件需要访问服务器上生成的文件夹。 To access the right folder the file needs the generated uniqID. 要访问正确的文件夹,文件需要生成的uniqID。

I tried it with Sessions. 我在Sessions上尝试过。 But that's a bad idea, because the user can make multiple Requests to the Controller in other Tabs and with Sessions the uniqID always gets overwritten. 但这是一个坏主意,因为用户可以在其他选项卡中向控制器发出多个请求,并且使用会话时,uniqID总是会被覆盖。

How can i pass the uniqID from my Controller to the PHP File? 如何将uniqID从控制器传递到PHP文件?

There are a number of approaches to do this task. 有许多方法可以完成此任务。

Here are few of them that I quickly came up with: 以下是我很快想到的一些建议:

1) Store the key value mapping in database (key as the id, value as the unique number) 1)将键值映射存储在数据库中(键作为id,值作为唯一编号)

2) You can do that in sessions too because every id will have a unique entry, a session having an array of KEY (id) => VALUE (unique id) is fine too but to avoid over head and consistency, I would recommend you to save the mapping in database. 2)您也可以在会话中执行此操作,因为每个id都有一个唯一的条目,一个会话包含KEY (id) => VALUE (unique id)的数组也可以,但是为了避免开销和一致性,我建议您将映射保存在数据库中。

Cons: If there server is restarted, yon won't have any clue about the session information. Cons:如果重新启动服务器,则yon不会提供有关会话信息的任何线索。 It is not a good practice though. 不过,这不是一个好习惯。

3) You can use filing but again you need to write proper functions to retrieve and store the mappings. 3)您可以使用归档,但是再次需要编写适当的函数来检索和存储映射。

You could post the data to the file with PHPCURL . 您可以使用PHPCURL将数据发布到文件中。 For example: 例如:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'yourfile.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $uniqueID['id']);
curl_exec($ch);
curl_close($ch);

In yourfile.php, you can fetch the data: 在yourfile.php中,您可以获取数据:

if($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_POST['id'])){
    //execute code...
}

You can store the user_id and the unique_folder_id in a database table and do a lookup in the database from the php file and create the directories. 您可以将user_id和unique_folder_id存储在数据库表中,并通过php文件在数据库中进行查找并创建目录。

You can add a flag done taking 0 and 1 values to check if the folder has been created already. 您可以添加一个标志done以0和1的值,以检查该文件夹已被创建。
Or if the amount of records permits, you could check with file_exists before attempting to create the directory. 或者,如果记录量允许,则可以在尝试创建目录之前与file_exists进行检查。

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

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