PHP Syntax Error? - php

I have coded a nice script but i am constantly getting
Error on line 29: Parse error, unexpected T_IF(if)
I have tried debugging code, wasted plenty of time. But nothing, came out.
Here is my code.
<?php
include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, "$ip");
$referrer=$_SERVER['HTTP_REFERER'];
// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");
$real=0;
geoip_close($gi);
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
}
else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time()+315360000);
if ($country_code="IN") {
if(preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY='.$key.'&IP='.$ip);
$real=$result
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
{setcookie("testcookie", "testvalue");
if( isset( $_COOKIE['testcookie'] ) ) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs){
$real=1;
}
}
else
$real=1;
}
else
$real=1;
} else
$real = 1;
}
else {
$real = 1;
}
} }
if ($real==1) {
include_once('Biggenius1.htm');
}
?>
It is if inside. Please give me advice, on how can i avoid these error. And also is there any alternative to code such complex script with multiple nested if statements?
Please post entire code:

try this
$real = 0;
geoip_close($gi);
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
} else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time() + 315360000);
if ($country_code = "IN") {
if (preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY=' . $key . '&IP=' . $ip);
$real = $result;
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's {
if ($real == 0) {
setcookie("testcookie", "testvalue");
if (isset($_COOKIE['testcookie'])) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
}
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs) {
$real = 1;
}
}
else
$real = 1;
}
else
$real = 1;
} else
$real = 1;
}
else {
$real = 1;
}
}
if ($real == 1) {
include_once('Biggenius1.htm');
}

On line 29, $real=$result should end in a semi-colon and on the following line {if($real==0) should be if($real==0){.
The error message is your friend, it suggested you look to line 29.

You placed a curely braces before the if condition
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
remove it then your error wil be removed

From reading over your code, it seems like the only errors I can find are these:
{if($real==0)
And:
$real=$result
Which should be changed into:
if($real==0){
And:
$real=$result;

Here are the few errors I found:
if ($country_code="IN") : This is an assignment not comparision, will always return true
$real=$result : Missing Termination ; on the end

Related

Cannot Redirect Using $_SERVER Variables, PHP

I have a PHP application (a request form) that first checks for an active $_SESSION before a user can access the site. Because of the timeout period set for this form there is rarely an active session. Here's the check:
if (isset($_SESSION['samlUserdata'])) {
$attributes = $_SESSION['samlUserdata'];
$user_department = $attributes['department'];
$user_email = $attributes['email'];
$user_employee_id = $attributes['employee_id'];
$user_full_name = $attributes['full_name'];
}
...and here is the else {} that I use to grab the REQUEST_URI:
else {
if (isset($_SERVER['REQUEST_URI'])) {
$referer = $_SERVER['REQUEST_URI'];
$redirect = "https://myinternalwebsite.net$referer";
}
header("Location: https://myinternalwebiste.net/confirm_auth.php?sso");
}
...and last, here is what I do with the $_GET
if (isset($_GET['sso'])) {
if (isset($redirect)) {
$auth->login($redirect);
} else {
$auth->login("https://myinternalwebsite.net/");
}
}
However, once my session is killed I am never properly routed back to the URL set in the ['REQUEST_URI'], I am always just dumped onto the internal site's front page. I have troubleshooted this on and off for some time over the last week, to no avail. I've tried other variables in the $_SERVER array as well, such as ['REDIRECT_URL'].
I'm at a loss, and I'm sure this fairly simple for anyone with more experience than myself... so I am all ears and eager to learn.
EDIT:
Thank you for the comments below. Per your advice I will add the entirety of my code here, removing only the unnecessary parts. (And yes, I appreciate the tip to flip the initial (isset()) to (!isset(). Thank you for that.)
<?php
session_start();
$auth = new OneLogin\Saml2\Auth($saml_settings);
if (isset($_SESSION['samlUserdata'])) {
$attributes = $_SESSION['samlUserdata'];
$user_department = $attributes['department'];
$user_email = $attributes['email'];
$user_employee_id = $attributes['employee_id'];
$user_full_name = $attributes['full_name'];
} else {
if (isset($_SERVER['REQUEST_URI'])) {
$referer = $_SERVER['REQUEST_URI'];
$redirect = "https://example.net$referer";
}
header("Location: https://example.net/confirm_auth.php?sso");
}
if (isset($_GET['sso'])) {
if (isset($redirect)) {
$auth->login($redirect);
} else {
$auth->login("https://example.net/");
}
} else if (isset($_GET['slo'])) {
$auth->logout();
} else if (isset($_GET['acs'])) {
$auth->processResponse();
$errors = $auth->getErrors();
if (!empty($errors)) {
echo '<p>', implode(', ', $errors), '</p>';
}
if (!$auth->isAuthenticated()) {
echo "<p>Not authenticated!</p>";
exit();
}
$_SESSION['samlUserdata'] = $auth->getAttributes();
if (isset($_POST['RelayState']) &&
OneLogin\Saml2\Utils::getSelfURL() != $_POST['RelayState']) {
$auth->redirectTo($_POST['RelayState']);
}
} else if (isset($_GET['sls'])) {
$auth->processSLO();
$errors = $auth->getErrors();
if (empty($errors)) {
echo '<p>Sucessfully logged out!</p>';
} else {
echo '<p>', implode(', ', $errors), '</p>';
}
}
?>

php validation from array form

I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}

Javascript confirm() function cannot pass parameters properly

I'm using PHP and JavaScript, and I got a problem when deal with the confirm() function in JavaScript.
Say I have a page add.php, firstly I receive some parameters passed from another page, and I check to see if they are valid or not. If yes, I just insert the data into db and return to another page, if they are not valid, there'll be a confirm() window popped up and let the user to choose whether to continue or not. If the user still choose to continue, I want the page to be reloaded with all the parameters sent again. But the problems is that I cannot get the parameter the second time add.php is loaded.
Previously I didn't use a window.onload function and confirm() pop up, but an < a href> link instead, everything worked fine (Please see the attached code at the end). But when I tried to use the following code, the same url stopped working
echo "<script type=\"text/javascript\">";
echo "window.onload = function() {
var v = confirm(\"$name is not alive, do you want to add it into system?\");
if (v) {
window.location.href= \"add.php?type=room&name=$name&area\"
+ \"=$area&description=$description&\"
+ \"capacity=$capacity&confirm=Y\";
} else {
window.location.href= \"admin.php?area=$area\";
}
}";
echo "</script>";
Following is the previous version, instead of using window.onload(), I used < a href="..." /> link, everything worked fine at that time. get_form_var is a function in functions.inc, which is to get the parameter using $_GET arrays.
<?php
require_once "functions.inc";
// Get non-standard form variables
$name = get_form_var('name', 'string');
$description = get_form_var('description', 'string');
$capacity = get_form_var('capacity', 'string');
$type = get_form_var('type', 'string');
$confirm = get_form_var('confirm','string');
$error = '';
// First of all check that we've got an area or room name
if (!isset($name) || ($name === ''))
{
$error = "empty_name";
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location: $returl");
}
// we need to do different things depending on if its a room
// or an area
elseif ($type == "area")
{
$area = mrbsAddArea($name, $error);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location: $returl");
}
elseif ($type == "room")
{
if (isset($confirm)){
$dca_osi = getOsiVersion($name);
$room = mrbsAddRoom(
$name,
$area,
$error,
$description,
$capacity,
$dca_osi,
1
);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location:$returl");
}
else {
$dca_status= pingAddress($name);
$dca_osi = getOsiVersion($name);
if( $dca_status == 0){
$room = mrbsAddRoom(
$name,
$area,
$error,
$description,
$capacity,
$dca_osi,
0
);
$returl = "admin.php?area=$area"
. (!empty($error) ? "&error=$error" : "");
header("Location:$returl");
}
else {
print_header(
$day,
$month,
$year,
$area,
isset($room) ? $room : ""
);
echo "<div id=\"del_room_confirm\">\n";
echo "<p>\n";
echo "$name is not alive, are you sure to add it into system?";
echo "\n</p>\n";
echo "<div id=\"del_room_confirm_links\">\n";
echo "<a href=\"add.php?type=room&name"
. "=$name&area=$area&description"
. "=$description&capacity=$capacity&confirm"
. "=Y\"><span id=\"del_yes\">"
. get_vocab("YES") . "!</span></a>\n";
echo "<a href=\"admin.php?area=$area\"><span id=\"del_no\">"
. get_vocab("NO") . "!</span></a>\n";
echo "</div>\n";
echo "</div>\n";
}
}
}
function pingAddress($host)
{
$pingresult = exec("/bin/ping -c 1 $host", $outcome, $status);
if ($status==0) {
return $status;
}
else {
return 1;
}
}
function getOsiVersion($host)
{
$community = 'public';
$oid = '.1.3.6.1.4.1.1139.23.1.1.2.4';
$sysdesc = exec("snmpwalk -v 2c -c $community $host $oid");
$start = strpos($sysdesc, '"');
if ($start!==false) {
$sysdesc = substr($sysdesc, $start+1,$sysdesc.length-1);
return $sysdesc;
}
else {
return "not available";
}
}
I've solved the problem, just simply by using "&" instead of " & amp;" in the url link... it works fine now...
You try location.reload() javascript call?

PHP Issue - function output is 000

I've been trying to make a CLI application that logs Yahoo! messenger login dates/times for certain users using a third party, but this isn't really getting anywhere. Even though checking iself works when used individually, it does not seem to when using the while & foreach too. checkAvailability outputs "000". Could anyone please fix this and perhaps optimize it?
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
echo $status_coded;
return $status_coded;
}
while(true) {
foreach($users as $user) {
$user['oldstatus'] = $user['status'];
if (checkAvailability($user) == "1" and $user['oldstatus'] != "online") {
$user['status'] = "online";
echo "online";
} elseif (checkAvailability($user) == "3" and $user['oldstatus'] != "invisible") {
$user['status'] = "invisible";
echo "invisible";
} elseif (checkAvailability($user) == "2" and $user['oldstatus'] != "offline") {
$user['status'] = "offline";
echo "offline";
} else {
$user['status'] = "error";
echo "error";
}
if ($user['status'] != $user['oldstatus']) {
echo $user." a fost detectat ca ".$user['status']." la ".date(DATE_RFC822).".\n";
}
}
sleep(60);
sleep(60);
}
An endless runing CLI application in PHP in not the best solution. Its better when you make a cronjob and run the script every one or every to minutes. Then you can store the status or what you need in a database or a file.
I have looked at your scripted and test the script:
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
return $status_coded;
}
while(true) {
foreach($users as $key => $user) {
$userStatus[$key] = checkAvailability($user);
if(!isset($userStatusRet[$key]['oldstatus'])) {
$userStatusRet[$key]['oldstatus'] = '';
}
if(!isset($userStatusRet[$key]['status'])) {
$userStatusRet[$key]['status'] = '';
}
$userStatusRet[$key]['oldstatus'] = $userStatusRet[$key]['status'];
if ($userStatus[$key] == "1" and $userStatusRet[$key]['oldstatus'] != "online") {
$userStatusRet['status'] = "online";
echo "User ".$key.": online\n";
} elseif ($userStatus[$key] == "3" and $userStatusRet[$key]['oldstatus'] != "invisible") {
$userStats['status'] = "invisible";
echo "User ".$key.": invisible\n";
} elseif ($userStatus[$key] == "2" and $userStatusRet[$key]['oldstatus'] != "offline") {
$userStatusRet[$key]['status'] = "offline";
echo "User ".$key.": offline\n";
} else {
$userStatusRet[$key]['status'] = "error";
echo "User ".$key.": error\n";
}
}
sleep(5);
}
Its not the best solution but the problem in your case is, that you can't write in the $user output. I have made here a new variable with the user id as iterator. When you run the script you can see the output:
User 0: online
User 1: offline
User 0: online
User 1: error
User 0: online
User 1: offline

trying to save time with PHP if/elseif statements

I have a rather big if statement:
if (!$result_spam)
{
$confrim_spam = "FAILED";
}
else if ($result_spam)
{
$confrim_spam = "PASSED";
}
if (!$result_email_manage)
{
$confrim_email_manage = "FAILED";
}
else if ($result_email_manage)
{
$confrim_email_manage = "PASSED";
}
if (!$result_analyt)
{
$confrim_analytics = "FAILED";
}
else if ($result_analyt)
{
$confrim_analytics = "PASSED";
}
Now I want to do another if statement to check if all have PASSED or if all have FAILED or is some have PASSED and some have FAILED and then echo (do something with) the failed ones.
I know how to check if all have passed or failed:
if ($confirm_spam == "PASSED" AND $confirm_analytics == "PASSED"
but to check if some have passed and some haven't and then find the ones that failed will take too long, right?
I was just wondering, would there be an easier/quicker way to do this?
Since they are all bools anyway:
if($result_spam && $result_email_manage && $result_analyt){
//do all passed
}
elseif($result_spam || $result_email_manage || $result_analyt){
//at least one passed
if(!$result_spam){ echo '$result_spam failed';}
if(!$result_email_manage){ echo '$result_email_manage failed';}
if(!$result_analyt){ echo '$result_analyt failed';}
}
else {
//do all failed
}
You can change validation logic to something like
$passed = array();
$failed = array();
if (!$result_spam)
{
array_push($failed, "confirm_spam");
}
else
{
array_push($passed, "confirm_spam");
}
...
Then you have an easy and clear way to check whether all passed/failed and which tests are failed.
What if you try this way:
$passed = $failed = "";
$all = array("confrim_spam" => $result_spam,
"confrim_email_manage" => $result_email_manage,
"confrim_analytics" => $result_analyt);
foreach($all as $a => $b)
{
if (!$b)
$failed.= $a . ", ";
else
$passed.= $a . ", ";
}
Then if var $passed is empty, none passed else if $failed is not empty, at last one have not passed.. so do you got what passed and what failed and do something with them. And you can store results both in a string or an array whatever you want...

Categories