PHP 如何处理 1 个文件上具有相同变量名的同时命中


How does PHP handle simultaneous hits with same variable names on 1 file

我有一个php文件,里面有API代码。这个文件正在使用推特API,在文件中,我通过会话传递用户密钥来处理和发送推文。

我想知道,当多个请求同时访问此文件时会发生什么?

如果来自不同地理位置的 5 个请求进入文件..每个都有不同的变量值...但所有人都将采用相同的变量名称...PHP 是否创建了该文件的 5 个实例来执行推文?

请查看我的代码以了解发生了什么:http://pastebin.com/ic6pYLgi

当多个人向该文件发送请求时会发生什么情况?

<?php
session_start();
//pass session variables into  standard variable
$ck = $_SESSION['consumer_key'];
$cs = $_SESSION['consumer_secret'];
$at = $_SESSION['access_token'];
$ats = $_SESSION['access_token_secret'];
$hashtag = $_SESSION['hashtag'];
$tweet = $_POST['tweet'];
//TODO unset session at end of script
//TODO limit tweet length
if (!empty($ck && $cs && $at && $ats && $tweet)){
  $ck = $_POST['consumer_key'] = $ck; //get session value into POST then pass back to variable
  $cs = $_POST['consumer_secret'] = $cs;
  $at = $_POST['access_token'] = $at;
  $ats = $_POST['access_token_secret'] = $ats;
  $_POST['tweet'];
  $hashtag = $_POST['hashtag'] = $hashtag;
}

require "autoload.php";
use Abraham'TwitterOAuth'TwitterOAuth;
define('CONSUMER_KEY', $ck); // add app consumer key between single quotes
define('CONSUMER_SECRET', $cs); // add app consumer secret key between single quotes
$access_token = $at;
$access_token_secret = $ats;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
if (!empty($ck && $cs && $at && $ats && $tweet && $hashtag)) {
  $hashtag = '#' . $hashtag;
  $connection->post('statuses/update',array('status' => $tweet . ' ' . $hashtag));
  $t = $connection;
  echo '<pre>';
  //print_r($t);
  echo '</pre>';
  echo "Your message has been sent to Twitter with a hashtag.";
}
//TODO Add error handling to send to special inbox

//Tweet without hashtags
if (!empty($ck && $cs && $at && $ats && $tweet) && empty($hashtag)) {
  $connection->post('statuses/update',array('status' => $tweet));
  //$t = $connection;
  //echo '<pre>';
  //print_r($t);
  //echo '</pre>';
  //echo "Your message has been sent to Twitter without a hashtag.";
}
session_destroy(); //this will hopefully help with load on this file
?>

php 请求中的任何变量都独立于任何并发请求,因此正在处理的其他请求绝不会对您的变量进行编辑。

$_SESSION var 将包含共享会话范围的副本。 在调用 session_start() 时复制一次,即使有其他同时处理的页面,它们也不会对 $_SESSION 的内容产生任何影响

不过,您可能不会同时进行处理。 据我了解,PHP 无法处理比服务器上的处理器更多的同时页面。 如果您有一台处理器服务器,则不会同时处理页面,当另一个页面正在处理时,页面请求会排队等待。