【问题标题】:Access the content of page if session is activated else activate session first如果会话被激活,则访问页面的内容,否则首先激活会话
【发布时间】:2016-12-21 14:03:48
【问题描述】:

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

<?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>

【问题讨论】:

  • 检查您的 PHP 日志。您的会话登录页面至少有一个明显的语法错误。您在服务器端代码中输入了一个随机 URL,在 input 元素内。
  • @David (program):5 Uncaught TypeError: Cannot read property 'removeAttribute' of nullprotectStyleSheet @ include.preload.js:393 先生这是什么意思?
  • 如果您已经发送了内容(例如通过echo),则不能使用header
  • @apokrypos Bro 那是正确重定向我的问题在其他地方
  • 可能是因为你在php之前发送了输出header

标签: php html session


【解决方案1】:

你可以试试这个脚本代替标题

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

【讨论】:

  • 谢谢老哥解决了 能给我推荐什么好的教程或书籍吗
  • 我也在学习。我基本上是从 youtube 或 udemy 教程中学习的
  • 是的兄弟,我在这里问问题对我有很大帮助
猜你喜欢
  • 2017-01-08
  • 2019-12-24
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多