502 Bad Gateway error

Hello,

im using nginx at my server.

when a user visit this page , he seed two option :

1)Enter his mail and click the Validate button,
2) click on the "Facebook connect ", he gets redirected to a new page, then he sees "Please like our page in order to continue" with the like button underneath , and when he clicks on the like button =>(this part making the error)  he should login using guest:guest in to my system and redirect me to the website i set in the code using this line:
 $userurl    = 'http://www.mywebsite.com';

Open in new window


I think i gets in a loop and then it giving me the error

502 Bad Gateway
nginx/1.1.19


here is de code:

<?php
include_once('include/webzone.php');
include_once('include/presentation/header.php');

$email = $_GET['email'];
$force_connect = 1;

?>

<div class="container">
	<br>
	
	<?php
	$f1 = new Fb_ypbox();
	$user_data = $f1->getUserData();
	$token = $f1->getAccessToken();
	
	$url = 'https://graph.facebook.com/'.$user_data['id'].'/likes?access_token='.$token;
	$data = $f1->getDataFromUrl($url);
	$data = json_decode($data, true);
	$data = $data['data'];
	$liked = 0;
	for($i=0; $i<count($data); $i++) {
		if($GLOBALS['like_page_id']==$data[$i]['id']) $liked=1;
	}
	
	if(count($user_data)>0) {
		$force_connect=0;
		$users = get_users(array('fb_user_id'=>$user_data['id']));
		if(count($users)>0) {
			if($users[0]['fb_token_expires']!=0 && time()>$users[0]['fb_token_expires']) {
				$force_connect=1;
			}
		}
		else {
			$force_connect=1;
		}
	}
	
	if($email!='') $force_connect=0;
	
	echo '<div id="locked_content">';
	
		if($force_connect==1) {
			echo '<h1>Start here !</h1>';
			echo '<form>';
			echo 'Email:<br><input type="text" id="email" name="email" style="width:240px;"><br>';
			echo '<input type="submit" value="Validate">';
			echo '</form>';
			echo '<br><a href="./account/fb_connect.php" style="font-size:20px;">Facebook connect</a>';
		}
		else {
			
			if(($liked && count($user_data)>0) || $email!='') {
				
				if($email!='') {
					$user = get_users(array('email'=>$email));
					if(count($user)==0) {
						$m1 = new MySqlTable();
						$sql = 'INSERT INTO '.$GLOBALS['db_table']['users'].' (fb_email, created) VALUES ("'.$email.'", "'.date('Y-m-d H:i:s').'")';
						$m1->executeQuery($sql);
					}
				}
				
				//function after like button is hit
				$result = get_settings();
	for($i=0; $i<count($result); $i++) {
		$settings[$result[$i]['meta_key']] = $result[$i]['meta_value'];
	}
	$challenge = $_REQUEST['challenge'];
	$userurl   = $_REQUEST['userurl'];
	$res	   = $_REQUEST['res'];
	$qs        = $_SERVER["QUERY_STRING"];

    $uamip     = $_REQUEST['uamip'];
    $uamport   = $_REQUEST['uamport'];
    $mac_address   = $_REQUEST['mac'];


    //--There is a bug that keeps the logout in a loop if userurl is http%3a%2f%2f1.0.0.0 ---/
    //--We need to remove this and replace it with something we want
    if (preg_match("/1\.0\.0\.0/i", $userurl)) {
        $default_site = 'google.com';
        $pattern = "/1\.0\.0\.0/i";
        $userurl = preg_replace($pattern, $default_site, $userurl);
    }
    //---------------------------------------------------------

	if($res == 'success'){

		header("Location: $userurl");
		exit();
		//print("\n</html>");
	}

	if($res == 'failed'){

		header("Location: /hotspot/fail.php?".$qs);
		exit();
		//print("\n</html>");

	}

   
    if($res == 'notyet'){

					$uamsecret  = 'greatsecret';
					$dir        = '/logon';
					$userurl    = 'http://www.mywebsite.com';
					$redir      = urlencode($userurl);

					// Just use a user created for guest usage, e.g username: guest, password: guest
					$username   = 'guest';
					$password   = 'guest';
					$enc_pwd    = return_new_pwd($password,$challenge,$uamsecret);
					$target     = "http://$uamip".':'.$uamport.$dir."?username=$username&password=$enc_pwd&userurl=$redir";
					header("Location: $target");
					exit();
					//print("\n</html>");
					$res = 'success';
				} else {
					header("Location: index.php?".$qs);
					exit();
					//print("\n</html>");
				}
				
			}
			
			else {
				echo '<div id="like_section">';
				echo '<h2>Please like our page in order to continue</h2>';
				echo '<fb:like href="'.$GLOBALS['like_page_url'].'" width="450" show_faces="true" send="false"></fb:like>';
				echo '</div>';
			}
		}
	
	echo '</div>';
	 function return_new_pwd($pwd,$challenge,$uamsecret){
            $hex_chal   = pack('H32', $challenge);                  //Hex the challenge
            $newchal    = pack('H*', md5($hex_chal.$uamsecret));    //Add it to with $uamsecret (shared between chilli an this script)
            $response   = md5("\0" . $pwd . $newchal);              //md5 the lot
            $newpwd     = pack('a32', $pwd);                //pack again
            $password   = implode ('', unpack('H32', ($newpwd ^ $newchal))); //unpack again
            return $password;
    }
	?>

</div>

<?php
include_once('include/presentation/footer.php');
?>

Open in new window




the part that makes the error display is:

$challenge = $_REQUEST['challenge'];
	$userurl   = $_REQUEST['userurl'];
	$res	   = $_REQUEST['res'];
	$qs        = $_SERVER["QUERY_STRING"];

    $uamip     = $_REQUEST['uamip'];
    $uamport   = $_REQUEST['uamport'];
    $mac_address   = $_REQUEST['mac'];


    //--There is a bug that keeps the logout in a loop if userurl is http%3a%2f%2f1.0.0.0 ---/
    //--We need to remove this and replace it with something we want
    if (preg_match("/1\.0\.0\.0/i", $userurl)) {
        $default_site = 'google.com';
        $pattern = "/1\.0\.0\.0/i";
        $userurl = preg_replace($pattern, $default_site, $userurl);
    }
    //---------------------------------------------------------

	if($res == 'success'){

		header("Location: $userurl");
		exit();
		//print("\n</html>");
	}

	if($res == 'failed'){

		header("Location: /hotspot/fail.php?".$qs);
		exit();
		//print("\n</html>");

	}

   
    if($res == 'notyet'){

					$uamsecret  = 'greatsecret';
					$dir        = '/logon';
					$userurl    = 'http://www.mywebsite.com';
					$redir      = urlencode($userurl);

					// Just use a user created for guest usage, e.g username: guest, password: guest
					$username   = 'guest';
					$password   = 'guest';
					$enc_pwd    = return_new_pwd($password,$challenge,$uamsecret);
					$target     = "http://$uamip".':'.$uamport.$dir."?username=$username&password=$enc_pwd&userurl=$redir";
					header("Location: $target");
					exit();
					//print("\n</html>");
					$res = 'success';
				} else {
					header("Location: index.php?".$qs);
					exit();
					//print("\n</html>");
				}

Open in new window



What this should do is : it should log me in to the my system using the $username   = 'guest'; $password   = 'guest';




When i use the code in a simple webpage like this its working:

<?php
include('db.php');
	$challenge = $_REQUEST['challenge'];
	$userurl   = $_REQUEST['userurl'];
	$res	   = $_REQUEST['res'];
	$qs        = $_SERVER["QUERY_STRING"];

    $uamip     = $_REQUEST['uamip'];
    $uamport   = $_REQUEST['uamport'];
    $mac_address   = $_REQUEST['mac'];


    //--There is a bug that keeps the logout in a loop if userurl is http%3a%2f%2f1.0.0.0 ---/
    //--We need to remove this and replace it with something we want
    if (preg_match("/1\.0\.0\.0/i", $userurl)) {
        $default_site = 'google.com';
        $pattern = "/1\.0\.0\.0/i";
        $userurl = preg_replace($pattern, $default_site, $userurl);
    }
    //---------------------------------------------------------

	if($res == 'success'){

		header("Location: $userurl");
		print("\n</html>");
	}

	if($res == 'failed'){

		header("Location: /hotspot/fail.php?".$qs);
		print("\n</html>");

	}

   
    if($res == 'notyet'){

		// Validate Form and login User
		if(isset($_POST['submit'])){
			$email = trim($_POST['email']);
			$agree = $_POST['agree'];
			$validate_email = filter_var($email, FILTER_VALIDATE_EMAIL);
			// var_dump($agree);
			// exit();
			if($validate_email !== false && $agree == 1){
				$access_date = date('Y-m-d H:i:s');
				$q = mysql_query("INSERT INTO promo_users VALUES (NULL, '$email', '$mac_address', '$access_date')");
				if($q){
					$uamsecret  = 'greatsecret';
					$dir        = '/logon';
					$userurl    = 'http://www.mywebsite.com';
					$redir      = urlencode($userurl);

					// Just use a user created for guest usage, e.g username: guest, password: guest
					$username   = 'guest';
					$password   = 'guest';
					$enc_pwd    = return_new_pwd($password,$challenge,$uamsecret);
					$target     = "http://$uamip".':'.$uamport.$dir."?username=$username&password=$enc_pwd&userurl=$redir";
					header("Location: $target");
					print("\n</html>");
				} else {
					header("Location: index.php?".$qs);
					print("\n</html>");
				}
			} else {
				header("Location: index.php?".$qs);
				print("\n</html>");
			}
		}
	
    }
    //Function to do the encryption thing of the password
    function return_new_pwd($pwd,$challenge,$uamsecret){
            $hex_chal   = pack('H32', $challenge);                  //Hex the challenge
            $newchal    = pack('H*', md5($hex_chal.$uamsecret));    //Add it to with $uamsecret (shared between chilli an this script)
            $response   = md5("\0" . $pwd . $newchal);              //md5 the lot
            $newpwd     = pack('a32', $pwd);                //pack again
            $password   = implode ('', unpack('H32', ($newpwd ^ $newchal))); //unpack again
            return $password;
    }

?>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Demo Promo</title>
</head>
<body>

<form action="" method="post">
	<h4>Enter your email address below to redeem access!</h4>
	<p>
		<label>Email Address:</label>
		<input type="text" name="email">
	<p>
	<p>
		<input type="checkbox" name="agree" value="1">
		<label>I have read and I agree with your <a href="terms.php">terms and conditions</a>, take me to the internet!</label>
	</p>
	<p>
		<input type="submit" name="submit" value="Redeem Access">
	</p>

</form>

</body>
</html>

Open in new window


in this page it works perfect and dont give any error.
Babak01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
502 is a downstream error on the server.  Either the connection is failing because the firewall or proxy is not forwarding correctly, or the server is overloaded.


I would say the server is just not going t accept the request the way you are formatting it, and you need to contact them to find out what the url has to look like.

Cd&
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Babak01Author Commented:
its my own server, and when i use the code separately in other page it works, but if i use it in the page i give above, it give me the 502 message
0
COBOLdinosaurCommented:
Right it is a down stream error your server is reporting an error trying to do the external connect.  

Cd&
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.