0

Hello I am having a bit of trouble with my php code. I have a Moodle site and I am able to grab user information. 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
  4. Then I take users "userid" and combine it with the MD5 seed to concatenate them together.
  5. I set the newly generated MD5 HAS cookie to expire in 30 seconds
  6. I verify cookie conditions (if user is not logged in the userid is 0)
  7. If all conditions are met they will be directed to the $locations variable address using header redirect.
  8. If conditions are not met they are redirect to a 404 page.

I am always getting the 404. Thank You in advance for any help.

<?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");

?>

3 Answers 3

0

Your if statement will always evaluate to false because this section always evaluates to 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?

1
  • My intention was to randomly generate a cookie tag the UID to it then make it expire in n-th time, while using variables for the firstname and lastname if they exist condition is met (verification) redirect using the $location variable. The redirect is actually going to an API verification page which will use the firstname and lastname for a process. I thought the !== was same as. Mar 2, 2019 at 9:07
0

I believe fixing the below section will solve the problem.

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

What about ?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.