PHP代码 - 保存变量,而不仅仅是一台PC上的cookie

时间:2017-03-15 13:39:16

标签: javascript php jquery html

我的一个项目是让某人能够在此HTML页面上选择选项,然后让该用户单击“输入”并更新新选项卡的URL。关键是将信息保存到PHP会话中,这样如果其他人输入相同的PHP URL,变量仍然会出现在消息中,而不仅仅保存在一台PC上。这是我需要帮助的地方。

有人可以查看我的PHP代码并告诉我我做得不对吗?

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<script language="javascript" type="text/javascript">
			function dynamicdropdown(listindex) {
			  document.getElementById('senator').className = listindex;
			}
			</script>
			
			<style>
			optgroup {
			  display: none;
			}

			select.agriculture 
			optgroup.agriculture,
			select.appropriations 
			optgroup.appropriations
		
			{
			  display: block;
			}
			
			div#header{
			padding: 1px;
			color: yellow;
			padding-left: 9px;
			background-color: #000080;
			}
			
			.category_div{
			padding: 3px;
			}
			
			.sub_category_div{
			padding: 3px;
			}
			
			.microphone{
			padding: 3px;
			}
			
			.category_div_1{
			padding: 3px;
			}
			
			.body{
			padding-right: 5px;
			}

			</style>
	</head>
	
<body>

	<div class="header" id="header">
	<h1>Testing Header</h1>
	</div>

	<div class="room130">
	<h3>Room 130</h3>
	<form target="Room 130" action = "test.php" method="POST">
		<div class="category_div" id="category_div">Committee:
			<select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
				<option value="">Select Committee</option>
				<option value="agriculture">AGRICULTURE</option>
				<option value="appropriations">APPROPRIATIONS</option>
			</select>
		</div>
		
		<div class="sub_category_div" id="sub_category_div">
		Individual:
			<select name="senator" id="senator">
				<option value="">Select individual</option>
					<optgroup class="agriculture">
						<option value="THE CHAIR">THE CHAIR</option>
						<option value="THE PRESENTER">THE PRESENTER</option>
					</optgroup>
			</select>
		</div>
		
		<div class="microphone" id="microphone">Microphone:
			<select id="microphone" name = "microphone">
				<option value=" "> </option>
				<option value="ON">ON</option>
				<option value="OFF">OFF</option>
			</select>
		</div>
		
			<input type="submit" value="Submit">
		
		</form>
	</div>

PHP代码:

<!DOCTYPE html>
<html>
<head>
	<title>Room 130</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"> 
	$(document).ready( function() {
	$('#java').delay("90000").fadeOut();
		});
</script>
<style>
	body {
			background-color: #000080;
			color: white;
			font-size: 72px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-weight: bold;
	}
	
	.java {
		width: 90%;
		margin: 200px;
		padding-top: 100px;
		text-align: center;
	}
</style>
</head>
<body>

<div class="java" id="java">
	<?php 
	
	$_SESSION["senator"] = $_POST["senator"];
	$_SESSION["microphone"] = $_POST["microphone"];
	
	echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you."; 
	
	?>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

首先,要存储和检索会话变量,您需要使用session_start()启动会话。

其次,会话标识符存储在本地cookie中,任何试图绕过它并为多个用户使用相同会话的尝试都会带来严重的麻烦和可能的安全漏洞,这是不值得的。

如果需要在服务器端存储值,请考虑使用数据库。对于非常简单的任务,您也可以使用平面文本存储,但通常不建议这样做。

现在,当谈到你想要实现的特定任务时,看起来HTTP GET变量正是你所需要的:

在第一页中替换此行:

<form target="Room 130" action = "test.php" method="POST">

有了这个:

<form target="Room 130" action="test.php" method="GET">

test.php中替换以下行:

$_SESSION["senator"] = $_POST["senator"];
$_SESSION["microphone"] = $_POST["microphone"];
echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you.";

有了这些:

$senator = $_GET['senator'];
$microphone = $_GET['microphone'];
echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";

如果不仅通过在第一页上提交表单来访问test.php,您可能需要检查变量是否存在:

if(isset($_GET['senator']) && isset($_GET['microphone'])) {
    $senator = $_GET['senator'];
    $microphone = $_GET['microphone'];
    echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";
} else {
    // Do something when the page is accessed without any GET variables in the url
}

如果您这样做,那么当您访问test.php?senator=Jon&microphone=12时,该页面将输出以下内容:

  

请提醒Jon确保他们的麦克风是12.谢谢。

编辑:另一种选择是文字存储。

在包含data.txt的同一文件夹中创建文件test.php,并设置0666的权限。

test.php中替换以下行:

$_SESSION["senator"] = $_POST["senator"];
$_SESSION["microphone"] = $_POST["microphone"];
echo "Please remind ". $_SESSION["senator"]. " to make sure their microphone is " .$_SESSION["microphone"]. ". Thank you.";

有了这些:

if(isset($_POST["senator"]) && isset($_POST["microphone"])) { // If the page receives POST data, it needs to be stored
    $senator = $_POST["senator"];
    $microphone = $_POST["microphone"];
    $data = json_encode(Array($senator, $microphone)); // Put values into an array and encode it for storage
    file_put_contents('data.txt', $data); // Put the JSON-encoded array into a text file. This function replaces the contents of the text file, which is exactly what is needed in this application. To append instead of replacing the contents, you need a FILE_APPEND flag.
} else { // If there's no POST data, the values are retrieved from the storage
    $data = json_decode(file_get_contents('data.txt')); // Retrieve the contents of the text file and decode them back into an array
    $senator = $data[0];
    $microphone = $data[1];
}
echo "Please remind ". $senator. " to make sure their microphone is " .$microphone. ". Thank you.";

编辑2:如果您使用上面的文本存储解决方案并希望使用JS动态更新页面,我建议使用jQuery。

由于我们已经将值存储在文本文件中,因此在JS中访问同一文件也是合乎逻辑的,尽管由于安全考虑而无法在本地访问它,因此必须像远程资源一样访问它。

将此JS功能添加到您的页面:

function updateData() {
    $.getJSON("https://website.com/data.txt", function(data) {
        var senator = data[0];
        var microphone = data[1];
        $("#java").text("Please remind " + senator + " to make sure their microphone is " + microphone + ". Thank you.");
    });
}

每当您想要更新数据时都要调用它。您可以将其置于循环中或单击时调用它。