我正在使用phpjobscheduler脚本我想发送带有$ id的链接但问题是链接是使用curl和include函数打开所以如何在数据库中添加link + id或者当它触发打开链接然后如何将$ id附加并使url像link + id ...第一页是将数据插入数据库的索引,这里是index.php代码....
$query="INSERT INTO data (scriptpath) VALUES ('http://localhost/test.php?id=')";
这是功能页面,我认为我们将使用id填写网址但不知道...代码位于下方
<?php
function fire_script($script,$id,$buffer_output=1)
{
if(($buffer_output) AND (!DEBUG)) ob_start();//buffer output
$scriptRunning = new scriptStatus;
$scriptRunning->script=$script;
if ($scriptRunning->Running($id) )
{
if (DEBUG) echo "<br>Now running: $script- id=$id (debug ref. 3.9b)<br>";
$start_time = microtime(true);
$fire_type = (function_exists('curl_exec') ) ? " PHP CURL " : " PHP fsockopen ";
// "://" satisfies both cases http:// and https://
if (strstr($script,"://") ) fire_remote_script($script);
else
{
include(LOCATION.$script);
$fire_type=" PHP include ";
}
if(($buffer_output) AND (!DEBUG))
{
$scriptRunning->output=ob_get_contents();
ob_end_clean();
}
if (!$buffer_output) $scriptRunning->output="";
$scriptRunning->execution_time=number_format( (microtime(true) - $start_time), 5 )." seconds via".$fire_type;
$scriptRunning->Stopped($id);
}
}
function Clear($id)
{
$dbc = dbc::instance();
//If things go wrong, or script timeout CLEAR script so will run next time
$result = $dbc->prepare("UPDATE table SET currently_running = '0' where id='$id' ");
$result = $dbc->execute($result);
}
class scriptStatus {
public $script;
public $output;
public $executionTime;
public function Running($id)
{
$dbc = dbc::instance();
$result = $dbc->prepare("UPDATE table SET currently_running='1' where id='$id' ");
$result = $dbc->execute($result);
register_shutdown_function('Clear', $id);//registered incase execution times out before scriptStatus->Stopped called
return $result; //register_shutdown_function always works, where destruct might not!
}
public function Stopped($id)
{
$dbc = dbc::instance();
$result = $dbc->prepare("UPDATE table SET currently_running='0' where id='$id' ");
$result = $dbc->execute($result);
if (ERROR_LOG) //save log to db
{
$now = time();
$this->script=clean_input($this->script);
$this->output=substr(htmlentities($this->output), 0, MAX_ERROR_LOG_LENGTH);// truncate output to defined length
$query="INSERT INTO ".LOGS_TABLE." (`id`, `date_added`,`script`, `output`, `execution_time`)
VALUES (NULL,'$now', '$this->script','$this->output','$this->execution_time') ";
$result = $dbc->prepare($query);
$result = $dbc->execute($result);
if (DEBUG) echo "<br>QUERY to insert data to ".LOGS_TABLE." table:<br>$query (debug ref. 3.9c)<br>";
}
}
}
function fire_remote_script($url)
{
$url_parsed = parse_url($url);
$scheme = $url_parsed["scheme"];
$host = $url_parsed["host"];
$port = isset($url_parsed["port"]) ? $url_parsed["port"] : 80;
$path = isset($url_parsed["path"]) ? $url_parsed["path"] : "/";
$query = isset($url_parsed["query"]) ? $url_parsed["query"] : "";
$user = isset($url_parsed["user"]) ? $url_parsed["user"] : "";
$pass = isset($url_parsed["pass"]) ? $url_parsed["pass"] : "";
$useragent="";
$referer=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$buffer="";
if (function_exists('curl_exec'))
{
$ch = curl_init($scheme."://".$host.$path);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_FAILONERROR,1); // true to fail silently
curl_setopt($ch, CURLOPT_AUTOREFERER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
curl_setopt($ch, CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
curl_setopt($ch, CURLOPT_USERPWD,$user.":".$pass);
$buffer = curl_exec($ch);
curl_close($ch);
}
elseif ( $fp = @fsockopen($host, $port, $errno, $errstr, 30) )
{
$header = "POST $path HTTP/1.0\r\nHost: $host\r\nReferer: $referer\r\n"
."Content-Type: application/x-www-form-urlencoded\r\n"
."User-Agent: $useragent\r\n"
."Content-Length: ". strlen($query)."\r\n";
if($user!= "") $header.= "Authorization: Basic ".base64_encode("$user:$pass")."\r\n";
$header.= "Connection: close\r\n\r\n";
fputs($fp, $header);
fputs($fp, $query);
if ($fp) while (!feof($fp)) $buffer.= fgets($fp, 8192);
@fclose($fp);
}
echo $buffer;
}
class dbc extends PDO
{
protected static $instance;
public function __construct()
{
$options = array(PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '".CHARSET."';"
);
try {
$this->dbconn = new PDO(DBDRIVER.":host=".DBHOST.";port=".DBPORT.";dbname=".DBNAME,DBUSER,DBPASS,$options);
return $this->dbconn;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function reportDBError($msg)
{
if (DEBUG) print_r('<div style="padding:10%;"><h3>'.nl2br($msg).'</h3>(debug ref. 3.9d)</div>');
else
{
if(!session_id()) session_start();
$_SESSION['mysql_errors'] = "\n\nDb error: ".$msg."\n";
}
}
public static function instance()
{
if (!isset(self::$instance)) self::$instance = new self();
return self::$instance;
}
public function prepare($query, $options = NULL) {
try { return $this->dbconn->prepare($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function bindParam($query) {
try { return $this->dbconn->bindParam($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function query($query) {
try {
if ($this->query($query)) return $this->fetchAll();
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); } }
public function execute($result) {//use for insert/update/delete
try { if ($result->execute()) return $result; }
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function executeGetRows($result) {//use to retrieve rows of data
try {
if ($result->execute()) return $result->fetchAll(PDO::FETCH_ASSOC);
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function __clone()
{ //not allowed
}
public function __destruct()
{
$this->dbconn = null;
}
}
我认为函数fire_remote_script($ url)是我们添加带有id的url的地方但问题是id没有在这个函数中定义。我可以修复这个或任何其他方法吗?
答案 0 :(得分:0)
是的,你必须有link.php?id = 4 not link + id link.php?NameOfVariable = VariableValue