如果会话被激活则访问页面内容否则首先激活会话

时间:2016-08-15 11:02:14

标签: php html5 session

请帮我解决这个问题。我想仅在激活会话时访问页面内容,否则重定向用户首先激活会话。但是当我将用户重定向到会话页面时,它会卡住并且无法返回到内容页面。我是新来的,所以请帮我解决这个问题。

<?php 
session_start();
if(!isset($_SESSION['username'])){
		echo "cookie is not activated" ;
		header('Location: http://localhost/CC/Loginsession.php');
		die;
}
else {
?>
<!doctype html>
<html>
<head>
<title>Update in PHP</title>

</head>
<body>


<?php
		$servername="localhost";
		$username="root";
		$password="";
		$conn=mysql_connect($servername,$username,$password);
	 
	if(!$conn ) {
     		die('Could not connect: ' . mysql_error());
	}

		$sq1 = 'select * from biodata';
		mysql_select_db('firstdb');
		$display=mysql_query($sq1,$conn);

	if(!$display ) {
	
				die('Could not get data: ' . mysql_error());
				exit;
	}

	if (mysql_num_rows($display) == 0) {
				echo "No rows found, nothing to print so am exiting";
				exit;
	}
?>
 

<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
      <thead>
        <tr>
          <th>ID</th>
          <th>Fname</th>
          <th>Lname</th>
          <th>Email</th>
          <th>Phone</th>
          <th>Message</th>
		  <th>Update</th>
        </tr>
      </thead>
      <tbody>
<?php
          while( $row = mysql_fetch_assoc( $display ) ){
            echo
            "<form method= 'post' />
				<tr>
					  <td ><input name='UID' value='{$row['ID']}' readonly/></td>
					  <td ><input name='upfname' value='{$row['fname']}' /></td>
					  <td ><input name='uplname' value='{$row['lname']}' /></td>
					  <td ><input name='upemail' value='{$row['email']}' /></td>
					  <td ><input name='upphone' value='{$row['phone']}' /></td>
					  <td ><input name='upmessage' value='{$row['message']}' /></td> 
					  <td><input type='Submit' name='update' value='Update' id='".$row["ID"]."' </td> 
			 	</tr>
			 </form>";
         	}
?>
      </tbody>
</table>
<?php
		if(isset($_REQUEST['update']))
		{
			
			$id   = $_REQUEST['UID'];
		 	$upfn = $_REQUEST['upfname'];
			$upln = $_REQUEST['uplname'];
		    $upem = $_REQUEST['upemail'];
			$upph = $_REQUEST['upphone'];
			$upms = $_REQUEST['upmessage'];
			
			$up="UPDATE biodata 
			SET
			fname='$upfn',
			lname='$upln',
			email='$upem',
			phone='$upph',
			message='$upms' 
			WHERE ID = $id";
			$updbb=mysql_query($up,$conn);
			
			if($updbb){
		header('Location: http://localhost/Prac/updateinsamepage.php');
}

	  
		}
}
?>
			
</body>
</html>>

和我的会话登录表单代码在这里

<?php 

session_start();

if(isset($_SESSION['username'])){
	echo "Already registered as $_SESSION[username]" ;
}
 
else if($_SERVER['REQUEST_METHOD'] == 'POST'){
	
	$uname=htmlentities($_POST['username']);
	$pass=htmlentities($_POST['password']);
	
		if(!empty($uname) && !empty($pass)) {
			 $_SESSION['username']=$uname;
			 echo "Thanks<br />" . "UserName: $uname " . "Password: $pass";
			 
			
		  }
	  
		 else{
	    	echo "Please fill out the both fields";
		 }
}
else {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Loginsession</title>
</head>

<body>
<form method="post">
Username:<input type="text" id="username" name="username" /> <br /><br />
Password:<input type="password" id="password" name="password"/><br /><br />
<input type="hidden" name="hiddenvalue" value="<?php http://localhost/CC/Loginsession.php?username=overwritten ?>"/>
<input type="Submit" value="Login" name="Submit" id="submit" />
</form>
<?php }?>
<?php
session_unset(); 
session_destroy(); 
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此脚本代替标题

echo '<script>window.location="localhost/CC/Loginsession.php"</script>';