如何在会话中使用 get 方法? [英] how to use get method in a session?
问题描述
这是我的代码.实际上,我正在页面上显示来自 mysql 的一些数据并创建动态链接.我想在开始任何代码之前在代码的最开始使用 session_start() 开始一个会话.我想存储要在其他页面上显示的链接的值..
here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td><a href = page2.php" . ">" . $row['usn'] . "</a>" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
推荐答案
您需要提供一个回退,以防提供的 URL 在 $_GET
部分中不包含正确的变量.
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET
section.
你有:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
如果 $_GET['submit']
未设置,您应该做其他事情:
You should do something else if $_GET['submit']
isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
您应该为您的 php 文件提供如下网址:
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
其中 333 是您要存储的值.
Where 333 is the value you want to store.
这篇关于如何在会话中使用 get 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!