मैं $_SESSIONS
पर नया हूं लेकिन इसे विभिन्न php फ़ाइलों के बीच चर का पुन: उपयोग करने की आवश्यकता है। नीचे दिए गए कोड में मैं एक और php फ़ाइल में परिवर्तनीय $word
का उपयोग करना चाहता हूं, लेकिन मुझे यह सुनिश्चित नहीं है कि यह कैसे करें।विभिन्न php फ़ाइलों के बीच सत्र चर का उपयोग करें
मेरे php फ़ाइल इस तरह दिखता है:
<?php
if (isset($_POST["search"])) {
//include database connection
$word = mysql_real_escape_string($_POST["search"]);
$word = htmlentities($word);
$sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
$results = mysql_query($sql);
if (mysql_num_rows($results)==0) {
echo $word, " text bla";
}else {
echo $word, " text bla bla";
while ($row = mysql_fetch_assoc($results)) {
echo '<pre>', print_r($row), '<pre>';
}
}
}?>
अपने सुझाव के लिए आगे देख रहे हैं।
--- अद्यतन सत्र अभी भी page2.php पर काम नहीं कर? ---
मुझे समझ में नहीं आता क्यों $_SESSION
काम नहीं करते हैं। एक page1.php मैं echo($_SESSION['word'])
कर सकते हैं और सही मान मिलता है, लेकिन एक page2.php मैं ('$'."_SESSION['word'] isn't set because you had never been at file one");
मैं सभी के लिए निम्न उपाय परीक्षण किया पाने लेकिन उनमें से कोई page2.php पर = एक ही परिणाम में काम किया।
मेरा पृष्ठ 1.php फ़ाइल।
<?php
session_start();
//if we got something through $_POST
if (isset($_POST["search"])) {
// include database connection
$connect = mysql_connect('localhost', 'root', 'NomiS123') or die(mysql_error());
mysql_select_db('workcard');
// sanitize user input
$word = mysql_real_escape_string($_POST["search"]);
$word = htmlentities($word);
// build search query to the database
$sql = ("SELECT task_id, task_date FROM customer JOIN task ON customer.id = task.customer_id WHERE mobil = $word ORDER BY task_date DESC LIMIT 0, 10");
// get results
$_SESSION['word'] = $word;
$results = mysql_query($sql);
if (mysql_num_rows($results)==0) {
$_SESSION['word'] = $word;
echo($_SESSION['word']. "<br>");
var_dump($_SESSION);
echo "<br>";
echo "link link <br>";
echo "<a href=\"../page2.php/\">new card</a> <br>";
echo "<a href=\"//cykelc/\">New Search</a>";
} else {
echo $word, " bla bla text <br> Create card <br>";
echo "Edit info on: ", $word, "<br>";
echo "<a href=\"//cykelc/\">New Search</a> <br>";
while ($row = mysql_fetch_assoc($results)) {
echo '<pre>', print_r($row), '<pre>';
}
//$results->free();
}
}
// mysql_close($connect);
?>
मेरे PAGE2.php फ़ाइल।
<?php
session_start();
if(isset($_SESSION['word'])) {
$word = $_SESSION['word'];
echo($word);
} else {
die('$'."_SESSION['word'] isn't set because you had never been at file one");
}
?>
मैं इस पर पागल हो रहा हूं।
अद्यतन - हल
मैं सभी नीचे दिए गए सुझावों का परीक्षण किया, लेकिन उनमें से कोई भी काम जो अजीब था, क्योंकि मैं सेट और page1.php और page2.php पर sesson_id()
बाहर गूंज सकता है, लेकिन पृष्ठ 2 पर। php मुझे एक अलग sesson_id()
मिला। मैंने अपनी एमएएमपी सत्र सेटिंग्स में देखना शुरू किया, लेकिन सब कुछ सही सेट था। समाधान पृष्ठ 2.php पर बहुत ऊपर session_start();
रखने के लिए "बस" था। और से बहुत ऊपर मैंने पहले सब कुछ भी <!DOCTYPE html>
आदि
हल + सबक सीखा :-)
आप गूगल में प्रतिबंध लगा दिया है? http://php.net/manual/en/session.examples.basic.php http://www.tizag.com/phpT/phpsessions.php – Cheery
कृपया, [mysql_ * 'functions] का उपयोग न करें (http : //stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), वे अब बनाए रखा नहीं जा रहे हैं और [आधिकारिक रूप से बहिष्कृत हैं] (https://wiki.php.net/ आरएफसी/mysql_deprecation)। इसके बजाय [तैयार बयान] (http://en.wikipedia.org/wiki/Prepared_statement) के बारे में जानें, और [पीडीओ] (http://us1.php.net/pdo) या [MySQLi] का उपयोग करें (http: // us1.php.net/mysqli)। [यह आलेख] (http://php.net/manual/en/mysqlinfo.api.choosing.php) आपको निर्णय लेने में मदद करेगा। –
@ चेरी I (हमेशा और) पहले से ही PHP मैनुअल के आधार पर पढ़ और परीक्षण कर चुके हैं। लेकिन यह काम नहीं कर सका। – Nomis