简体   繁体   English

PHP带条件重定向

[英]PHP redirect with conditions

Hello I am having a bit of trouble with my php code. 您好,我的php代码有点麻烦。 I have a Moodle site and I am able to grab user information. 我有一个Moodle网站,可以获取用户信息。 What I am trying to do: 我正在尝试做的是:

  1. My Steps of madness 我的疯狂步骤
  2. Grab first and last name of user (Variables are correct and work) 抓住用户的名字和姓氏(变量正确无误,可以正常工作)
  3. I am generating a Random seed using MD5 HASH and setting as the cookie 我正在使用MD5 HASH生成一个随机种子并将其设置为cookie
  4. Then I take users "userid" and combine it with the MD5 seed to concatenate them together. 然后,我将用户“ userid”与MD5种子组合在一起,以将它们连接在一起。
  5. I set the newly generated MD5 HAS cookie to expire in 30 seconds 我将新生成的MD5 HAS Cookie设置为在30秒后过期
  6. I verify cookie conditions (if user is not logged in the userid is 0) 我验证Cookie条件(如果未登录的用户ID为0)
  7. If all conditions are met they will be directed to the $locations variable address using header redirect. 如果满足所有条件,则将使用标头重定向将它们定向到$ locations变量地址。
  8. If conditions are not met they are redirect to a 404 page. 如果不满足条件,则将它们重定向到404页面。

I am always getting the 404. Thank You in advance for any help. 我总是收到404。在此先感谢您的帮助。

<?php
require('../../config.php');
global $USER;
/* Session Variables */
$userid = $USER->id;
$firstname=$USER->firstname;
$lastname=$USER->lastname;

/* Random MD5 seed to set as cookie */
$random = md5(rand(1,1000));
setcookie(MoodleSession, $random, time()+ 30, '/',"", 1);
$_COOKIE['MoodleSession'] = $random;
$randomcookie = $random;

/* Where I want to go if all conditions are true */
$location="Location: https://MyDomainHere.com/uid=".$firstname."_".$lastname;

/* Response Data and verification of MD5 with firstname and lastname */ 
if ($randomcookie."_".$userid !== $randomcookie."_".$userid && $firstname."_".$lastname != ""){
header($location); /* Redirect browser */
die();}

/* If condition are not met, user gets a 404 */
header("Location: https://MyDomainHere.com/404");

?>

Your if statement will always evaluate to false because this section always evaluates to False. 您的if语句将始终评估为false,因为此部分始终将评估为False。

$randomcookie."_".$userid !== $randomcookie."_".$userid

Are you trying to set the cookie on this page, then verify if the cookie on the page you are redirecting to, or are you trying to both set the cookie, and verify the cookie/login on this page? 您是要在此页面上设置cookie,然后验证是否要重定向到该页面上的cookie,还是要同时设置cookie并验证此页面上的cookie /登录名?

我相信解决以下部分将解决问题。

$randomcookie."_".$userid !== $randomcookie."_".$userid 

关于什么 ?

setcookie("MoodleSession", $random, time()+ 30, '/',"", 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM