Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何从其他文件访问其他变量?_Php_Variables_Get - Fatal编程技术网

PHP-如何从其他文件访问其他变量?

PHP-如何从其他文件访问其他变量?,php,variables,get,Php,Variables,Get,我有两个php文件:index.php和search_server.php。 我需要从index.php访问$pilihdomain,然后在search_server.php中使用它,在这行$resp=$uclassify->classify($tweet['text'],$pilihdomain,'herlambangp') 最后一天,我使用了require ang全局变量,但它似乎是错误的。 提前谢谢 //index.php <head> <title>Twi

我有两个php文件:index.php和search_server.php。 我需要从index.php访问$pilihdomain,然后在search_server.php中使用它,在这行
$resp=$uclassify->classify($tweet['text'],$pilihdomain,'herlambangp')

最后一天,我使用了require ang全局变量,但它似乎是错误的。 提前谢谢

//index.php

<head>
    <title>Twitter Search</title>
    <link href="search_client.css" type="text/css" rel="stylesheet" />
    <link href="tweet.css" type="text/css" rel="stylesheet" />
    <script src="jquery.min.js"></script>
    <script src="search_client.js"></script>
</head>
<body>
    <div id="search_box">
    <h1>Twitter Search</h1>
    <input name="search_terms" autofocus="autofocus"/>      
<?php 
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbname = "skripsi";
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql = mysql_query("SELECT * FROM namaklasifier");
while($row = mysql_fetch_array($sql)) {
    $clsfr = $row['username'];
    $sql = mysql_query("SELECT * FROM namaklasifier");
        echo '<select name="cmake" autofocus width="10">';
        echo '<option value="0">-Pilih Domain Klasifikasi-</option>';
        while($row = mysql_fetch_array($sql)) {
            echo '<option ' . ($clsfr==$row['username']) . ' value="'.$row['username'].'">'.$row['username'].'</option>'; 
    }
    echo '</select>';
}
?>
    <?php
$pilihdomain=$_POST['cmake'];
    ?>

推特搜索
推特搜索
//search_server.php

<?php 
if (!empty($_GET['q'])) {

    // Remove any hack attempts from input data
    $search_terms = htmlspecialchars($_GET['q']).' -smartfrencare -siapkan -klaim';

    // Get the application OAuth tokens
    require 'app_tokens.php';
    require_once("uClassify.php");
    $uclassify = new uClassify();
    // Set these values here
    $uclassify->setReadApiKey('8DvvfxwKPdvjgRSrtsTSOawmQ0');
    $uclassify->setWriteApiKey('v4Us59yQFhf9Z0nGrQsrTtzBI5k');
//   global $nilainet;
//   global $nilaineg;
//   global $nilaipos;

    // Create an OAuth connection
    require 'tmhOAuth.php';

    $connection = new tmhOAuth(array(
      'consumer_key'    => $consumer_key,
      'consumer_secret' => $consumer_secret,
      'user_token'      => $user_token,
      'user_secret'     => $user_secret
    ));

    // Request the most recent 100 matching tweets
    $http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
            array('q' => $search_terms,
                'count' => 50,
                'lang' => 'in',
                'locale' => 'jakarta',
                'type' => 'recent'));

    // Search was successful
    if ($http_code == 200) {

        // Extract the tweets from the API response
        $response = json_decode($connection->response['response'],true);
        $tweet_data = $response['statuses']; 

        // Load the template for tweet display
        $tweet_template= file_get_contents('tweet_template.html');

        // Load the library of tweet display functions
        require 'display_lib.php';  

        // Create a stream of formatted tweets as HTML
        $tweet_stream = '';
        foreach($tweet_data as $tweet) {

            // Ignore any retweets
            if (isset($tweet['retweeted_status'])) {
                continue;
            }
            // Get a fresh copy of the tweet template
            $tweet_html = $tweet_template;


            $resp = $uclassify->classify($tweet['text'], $pilihdomain, 'herlambangp');              
            $value = print_r($resp,true) ;          
            // Insert this tweet into the html
            $tweet_html = str_replace('[screen_name]',$tweet['user']['screen_name'],$tweet_html);
            $tweet_html = str_replace('[name]', $tweet['user']['name'],$tweet_html);        
            $tweet_html = str_replace('[profile_image_url]',$tweet['user']['profile_image_url'],$tweet_html);
            $tweet_html = str_replace('[tweet_id]', $tweet['id'],$tweet_html);
            $tweet_html = str_replace('[tweet_text]',linkify($tweet['text']),$tweet_html);
            $tweet_html = str_replace('[tweet_class]',$value,$tweet_html);
            $tweet_html = str_replace('[created_at]',twitter_time($tweet['created_at']),$tweet_html);
            $tweet_html = str_replace('[retweet_count]',$tweet['retweet_count'],$tweet_html);           

            // Add the HTML for this tweet to the stream
            $tweet_stream .= $tweet_html;
        }

        // Pass the tweets HTML back to the Ajax request
        print $tweet_stream;

    // Handle errors from API request
    } else {
        if ($http_code == 429) {
            print 'Error: Twitter API rate limit reached';
        } else {
            print 'Error: Twitter was not able to process that search';
        }
    }

} else {
    //not implement anything
}   

?>

您应该有一个外部“配置”或“设置”文件,其中包含您在站点中全局需要的变量。然后,在需要这些配置设置的所有页面中包含该外部文件

除了pilihdomain,我还为您将db设置放在这个文件中,因为您确实应该创建这些共享设置,而不是在每个需要它们的脚本中重新定义它们

settings.php

<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbname = "skripsi";
$pilihdomain = isset($_POST['cmake']) ? $_POST['cmake'] : '';
<?php require_once('settings.php'); ?>
<head>
    <title>Twitter Search</title>
    <link href="search_client.css" type="text/css" rel="stylesheet" />
    <link href="tweet.css" type="text/css" rel="stylesheet" />
    <script src="jquery.min.js"></script>
    <script src="search_client.js"></script>
</head>
<body>
    <div id="search_box">
    <h1>Twitter Search</h1>
    <input name="search_terms" autofocus="autofocus"/>      
<?php 
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql = mysql_query("SELECT * FROM namaklasifier");
while($row = mysql_fetch_array($sql)) {
    $clsfr = $row['username'];
    $sql = mysql_query("SELECT * FROM namaklasifier");
        echo '<select name="cmake" autofocus width="10">';
        echo '<option value="0">-Pilih Domain Klasifikasi-</option>';
        while($row = mysql_fetch_array($sql)) {
            echo '<option ' . ($clsfr==$row['username']) . ' value="'.$row['username'].'">'.$row['username'].'</option>'; 
    }
    echo '</select>';
}
?>
<?php require_once('settings.php'); ?>
<?php 
if (!empty($_GET['q'])) {

    // Remove any hack attempts from input data
    $search_terms = htmlspecialchars($_GET['q']).' -smartfrencare -siapkan -klaim';

    // Get the application OAuth tokens
    require 'app_tokens.php';
    require_once("uClassify.php");
    $uclassify = new uClassify();
    // Set these values here
    $uclassify->setReadApiKey('8DvvfxwKPdvjgRSrtsTSOawmQ0');
    $uclassify->setWriteApiKey('v4Us59yQFhf9Z0nGrQsrTtzBI5k');
//   global $nilainet;
//   global $nilaineg;
//   global $nilaipos;

    // Create an OAuth connection
    require 'tmhOAuth.php';

    $connection = new tmhOAuth(array(
      'consumer_key'    => $consumer_key,
      'consumer_secret' => $consumer_secret,
      'user_token'      => $user_token,
      'user_secret'     => $user_secret
    ));

    // Request the most recent 100 matching tweets
    $http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
            array('q' => $search_terms,
                'count' => 50,
                'lang' => 'in',
                'locale' => 'jakarta',
                'type' => 'recent'));

    // Search was successful
    if ($http_code == 200) {

        // Extract the tweets from the API response
        $response = json_decode($connection->response['response'],true);
        $tweet_data = $response['statuses']; 

        // Load the template for tweet display
        $tweet_template= file_get_contents('tweet_template.html');

        // Load the library of tweet display functions
        require 'display_lib.php';  

        // Create a stream of formatted tweets as HTML
        $tweet_stream = '';
        foreach($tweet_data as $tweet) {

            // Ignore any retweets
            if (isset($tweet['retweeted_status'])) {
                continue;
            }
            // Get a fresh copy of the tweet template
            $tweet_html = $tweet_template;


            $resp = $uclassify->classify($tweet['text'], $pilihdomain, 'herlambangp');              
            $value = print_r($resp,true) ;          
            // Insert this tweet into the html
            $tweet_html = str_replace('[screen_name]',$tweet['user']['screen_name'],$tweet_html);
            $tweet_html = str_replace('[name]', $tweet['user']['name'],$tweet_html);        
            $tweet_html = str_replace('[profile_image_url]',$tweet['user']['profile_image_url'],$tweet_html);
            $tweet_html = str_replace('[tweet_id]', $tweet['id'],$tweet_html);
            $tweet_html = str_replace('[tweet_text]',linkify($tweet['text']),$tweet_html);
            $tweet_html = str_replace('[tweet_class]',$value,$tweet_html);
            $tweet_html = str_replace('[created_at]',twitter_time($tweet['created_at']),$tweet_html);
            $tweet_html = str_replace('[retweet_count]',$tweet['retweet_count'],$tweet_html);           

            // Add the HTML for this tweet to the stream
            $tweet_stream .= $tweet_html;
        }

        // Pass the tweets HTML back to the Ajax request
        print $tweet_stream;

    // Handle errors from API request
    } else {
        if ($http_code == 429) {
            print 'Error: Twitter API rate limit reached';
        } else {
            print 'Error: Twitter was not able to process that search';
        }
    }

} else {
    //not implement anything
}   

?>

推特搜索
推特搜索
search\u server.php

<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbname = "skripsi";
$pilihdomain = isset($_POST['cmake']) ? $_POST['cmake'] : '';
<?php require_once('settings.php'); ?>
<head>
    <title>Twitter Search</title>
    <link href="search_client.css" type="text/css" rel="stylesheet" />
    <link href="tweet.css" type="text/css" rel="stylesheet" />
    <script src="jquery.min.js"></script>
    <script src="search_client.js"></script>
</head>
<body>
    <div id="search_box">
    <h1>Twitter Search</h1>
    <input name="search_terms" autofocus="autofocus"/>      
<?php 
$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql = mysql_query("SELECT * FROM namaklasifier");
while($row = mysql_fetch_array($sql)) {
    $clsfr = $row['username'];
    $sql = mysql_query("SELECT * FROM namaklasifier");
        echo '<select name="cmake" autofocus width="10">';
        echo '<option value="0">-Pilih Domain Klasifikasi-</option>';
        while($row = mysql_fetch_array($sql)) {
            echo '<option ' . ($clsfr==$row['username']) . ' value="'.$row['username'].'">'.$row['username'].'</option>'; 
    }
    echo '</select>';
}
?>
<?php require_once('settings.php'); ?>
<?php 
if (!empty($_GET['q'])) {

    // Remove any hack attempts from input data
    $search_terms = htmlspecialchars($_GET['q']).' -smartfrencare -siapkan -klaim';

    // Get the application OAuth tokens
    require 'app_tokens.php';
    require_once("uClassify.php");
    $uclassify = new uClassify();
    // Set these values here
    $uclassify->setReadApiKey('8DvvfxwKPdvjgRSrtsTSOawmQ0');
    $uclassify->setWriteApiKey('v4Us59yQFhf9Z0nGrQsrTtzBI5k');
//   global $nilainet;
//   global $nilaineg;
//   global $nilaipos;

    // Create an OAuth connection
    require 'tmhOAuth.php';

    $connection = new tmhOAuth(array(
      'consumer_key'    => $consumer_key,
      'consumer_secret' => $consumer_secret,
      'user_token'      => $user_token,
      'user_secret'     => $user_secret
    ));

    // Request the most recent 100 matching tweets
    $http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
            array('q' => $search_terms,
                'count' => 50,
                'lang' => 'in',
                'locale' => 'jakarta',
                'type' => 'recent'));

    // Search was successful
    if ($http_code == 200) {

        // Extract the tweets from the API response
        $response = json_decode($connection->response['response'],true);
        $tweet_data = $response['statuses']; 

        // Load the template for tweet display
        $tweet_template= file_get_contents('tweet_template.html');

        // Load the library of tweet display functions
        require 'display_lib.php';  

        // Create a stream of formatted tweets as HTML
        $tweet_stream = '';
        foreach($tweet_data as $tweet) {

            // Ignore any retweets
            if (isset($tweet['retweeted_status'])) {
                continue;
            }
            // Get a fresh copy of the tweet template
            $tweet_html = $tweet_template;


            $resp = $uclassify->classify($tweet['text'], $pilihdomain, 'herlambangp');              
            $value = print_r($resp,true) ;          
            // Insert this tweet into the html
            $tweet_html = str_replace('[screen_name]',$tweet['user']['screen_name'],$tweet_html);
            $tweet_html = str_replace('[name]', $tweet['user']['name'],$tweet_html);        
            $tweet_html = str_replace('[profile_image_url]',$tweet['user']['profile_image_url'],$tweet_html);
            $tweet_html = str_replace('[tweet_id]', $tweet['id'],$tweet_html);
            $tweet_html = str_replace('[tweet_text]',linkify($tweet['text']),$tweet_html);
            $tweet_html = str_replace('[tweet_class]',$value,$tweet_html);
            $tweet_html = str_replace('[created_at]',twitter_time($tweet['created_at']),$tweet_html);
            $tweet_html = str_replace('[retweet_count]',$tweet['retweet_count'],$tweet_html);           

            // Add the HTML for this tweet to the stream
            $tweet_stream .= $tweet_html;
        }

        // Pass the tweets HTML back to the Ajax request
        print $tweet_stream;

    // Handle errors from API request
    } else {
        if ($http_code == 429) {
            print 'Error: Twitter API rate limit reached';
        } else {
            print 'Error: Twitter was not able to process that search';
        }
    }

} else {
    //not implement anything
}   

?>

使用会话变量。
它们很容易设置,您可以跨页面使用它们。

很抱歉响应太晚,我会尽快尝试您的建议。谢谢:)你好@Steven,我已经尝试了代码,但是$pilihdomain尚未访问,这是错误消息“致命错误:未捕获的异常‘uClassifyException’,消息为‘文本应如何分类?’?似乎没有指定ClassifierName!'在C:\xampp\htdocs\kejar\code\uClassify.php:107堆栈跟踪:#0 C:\xampp\htdocs\kejar\code\search\u server.php(62):uClassify->classify(',,lah myn,,,“@…',NULL,'herlambangp')#1{main}在C:\xampp\htdocs\kejar code\uClassify.php的第107行抛出“@herlambangpermadi-该错误可能是由于没有post数据可设置
$pilihdomain.
。如果未设置
$\u POST['cmake']
,我修改了上面的答案,将
$pilihdomain
设置为空字符串(“”)。您好@Steven,感谢您的回复。settings.php中是否缺少某些内容?我发现“解析错误:语法错误,意外的';'”消息,我还不明白如何在php中使用“?”。谢谢史蒂文,再次编辑。我有一个额外的分号。嗨,在我的问题案例中有使用会话的例子吗?在你的例子中有很多代码,所以我不想给你的代码举个例子。不过,本文应该解释您需要了解的有关会话变量的所有信息:。正如我所说的,这些变量非常容易设置,了解它们的工作原理比我编写解决方案更值得。教一个人钓鱼等等。MySQL
函数的使用是不推荐的,你可以用MySQLi或者(更好的是IMO)
PDO
函数代替。另外,尝试将PHP逻辑与HTML分开。(谷歌:
关注点分离
)Hi@Luttekes,我必须将哪个MySql命令更改为MySQLi?就这些吗?有我的例子吗?谢谢。我不明白你为什么要编辑你的问题。我标记了它…请不要试图编辑您的问题并添加垃圾数据,以为其他人不会看到您的问题!此处清楚列出了您所做的编辑: