php if query works with wamp but not on live server - php

I'm using the code below to choose an image based on the domain.
the only difference between environments is wamp is running PHP 7 and my host 7.1 and I've been unable to get a copy of wamp with PHP 7.1 included.
on the live site, the end result is a blank white page with the PHP error on the 'else' line
I'm not sure if this is correct but after $logo_img = "3_logo.svg" if I add the semicolon you get a white page but without works just fine.
<?php
$host = $_SERVER['HTTP_HOST'];
$logo_img = '';
if($host == "1.co.uk") {
$logo_img = "1_logo.svg";
}
else if($host == "2.co.uk") {
$logo_img = "2_logo.svg";
}
else($host == "3.co.uk") {
$logo_img = "3_logo.svg" //with semicolon errors - unexpected ';'
}
?>
any help appreciated!!
UPDATE:
var_dump ($_SERVER['HTTP_HOST']); returns
Localhost
string 'localhost' (length=9)
Live site
string(23) "www.domain.co.uk"

Please fix this
here is syntax error
else($host == "3.co.uk") {
$logo_img = "3_logo.svg" //with semicolon errors - unexpected ';'
}
you need to write
elseif($host == "3.co.uk") {
$logo_img = "3_logo.svg";
}
else{ ..... }

Related

php: shellscript running before screen-output

I'm currently putting together a small web-based GUI to generate kickstart-scripts. I got a confirmation page that's sending the relevant data via POST to the PHP-page where the actual shell script is called to build the iso. So far it's working, but the page seems to execute the script before it outputs anything else (for example, the 'echo' I put in at the beginning of the page ...), and I'm absolutely clueless why. Would anyone care to enlighten me?
Here's the code to the PHP-page that's executing the shell script ...
echo 'Generating your ISO; this might take a while...';
sleep(20);
if (!isset($_POST['auth'])) {
$ad = 'N';
}
else {
$ad = 'Y';
}
if (!isset($_POST['oracle'])) {
$oracle = 'N';
}
else {
$oracle = 'Y';
}
if ((!isset($_POST['ip'])) or (!isset($_POST['hostname'])) or (!isset($_POST['rhsel'])) or (!isset($_POST['submit'])) or (!isset($_POST['gw'])) or (!isset($_POST['nm']))) {
die('Please use the correct form !');
}
if (isset($_POST['ip'])) {
$ip = trim($_POST['ip']);
}
if (isset($_POST['gw'])) {
$gw = trim($_POST['gw']);
}
if (isset($_POST['nm'])) {
$nm = trim($_POST['nm']);
}
if (isset($_POST['hostname'])) {
$hostname = trim($_POST['hostname']);
}
if (isset($_POST['rhsel'])) {
$rhsel = $_POST['rhsel'];
}
passthru("/usr/bin/sudo /data/skripte/webconfig.sh $rhsel $oracle $ad $ip $gw $nm $hostname 2>&1");
PHP scripts accessed via a browser are request-response, meaning all processing is done on the server prior to headers and content being sent to the client. This means you will not get a continually updating output like you would see on the command line. There is no way around this. Sorry.

PHP class getting error when using return

I have the following code:
public function lockFields($lockFieldsDB, $lockValue) {
if($lockFieldsDB == $lockValue){
$lockInputField = 'readonly="readonly"';
$lockSelectField = 'disabled = "disabled"';
$lockImage = '<img title="Locked field" src="http://www.trustive.com/static/images/padlock_icon.png">';
}
else {
$lockInputField = null;
$lockSelectField = null;
$lockImage = null;
}
return [$lockInputField, $lockSelectField, $lockImage];
}
The code used to work fine, but my host made some maintenance to his server and it seems that I am getting the following error in my php syntax error, unexpected '[' This refers to return [$lockInputField, $lockSelectField, $lockImage];
I can't seem to understand why it worked and now it doesn't after the update
Not sure about your server, but you might as well just do it the correct way.
Change this.
return [$lockInputField, $lockSelectField, $lockImage];
To this
return array($lockInputField, $lockSelectField, $lockImage);

syntax error in valid if statement

I have a PHP method which retrieves data either from memcache, or from the database if it is not yet available there.
Sometimes the data must be loaded from the DB to update the memcache, for example if something changes.
public function get_likes_per_user($u_id, $force_db = false) {
if ($this->memcache) {
$u_id = intval($u_id);
$key = 'liked_news_' . $u_id;
$res = $this->memcache->get($key);
if ($res != '') {
$res = unserialize($res);
}
// This line causes a syntax error for no obvious reason
if(!is_array($res) || $force_db){
$res = array();
$sql = "...";
$uu_query = $this->datenbank->query_result($sql);
while ($row = $uu_query->fetch_row()) {
$res[] = $row['uu_id'];
}
$this->memcache->set($key, serialize($res), 0, 3600);
}
return $res;
} else {
throw new Exception("Kein Memcache Objekt!");
}
}
For some reason I don't understand php decides to throw a syntax error for the if statement which decides whether or not to reload the data from the DB.
I tried different notations for this statement but I can't figure out what causes the error.
Did anyone encounter something similar?
PHP Version 5.4
UPDATE:
I've found the cause of the error. I am working on a mac and to write a pipe charakter you have to press alt+7. If you press alt+spacebar mac OS X will insert a non-breaking space, which is invalid for PHP. So my mistake was to keep alt pressed after typing the two pipes for the OR statement.

Php error Working with get varialbes

i have a php code and when i add some gets variables i get error, 500, i tested it with ajax and without ajax(directly writing the url on the adress bar)
When i go with localhost/ajax/test.php?did=1 everything is fine but when i go with localhost/ajax/test.php?did=1&task=1 the problem happens
all the functions are created before on the ../php/core.php
Here is the code
<?php
require '../php/core.php';
$did = floor($_GET['did']);
if (device_exist($did) && amlogedin() && intouch_get($uid, $did) == 2) {
$task = floor($_GET['task']);
$id = safestring($_GET['id']);
switch ($task) {
case 1:
if (feature_removefeature($did, $fid)) {
echo donemsg("Feature Removed");
}
break;
}
design_makefeturelist($did);
}
else {
echo 'Sorry, some thing is wrong';
}
Almost sure that you've got an error in the feature_removefeature or donemsg function. Because after you set task=1 it will enter the case 1: statement. You could replace the if with a simple echo "lala"; to check it out.
ps. Is $fid already set?

Adding a variable to PHP script causes another line to create an error

Quick question...
I'm using this code to communicate with my flash application:
<?php
// Only run this script if the sendRequest is from our flash application
if ($_POST['sendRequest'] == "parse") {
// Print the var to flash
print "var1=The user's name is Harry";
}
?>
Now this works fine but as soon as I add a variable:
<?php
// Only run this script if the sendRequest is from our flash application
if ($_POST['sendRequest'] == "parse") {
$uname = "Name"
// Print the var to flash
print "var1=The user's name is Harry";
}
?>
I get an error stating:
Parse error: syntax error, unexpected T_PRINT in /home/a4935911/public_html/usersOnline.php on line 7
Where line 7 is my print statement. Why is this happening??? Please can someone help. PHP is driving me crazy...
Missing semi-colon in $uname = "Name"
change it to $uname = "Name";
Rewrite:
<?php
// Only run this script if the sendRequest is from our flash application
if ($_POST['sendRequest'] == "parse") {
$uname = "Name";
// Print the var to flash
print "var1=The user's name is Harry";
}
?>
Added note: A semi-colon can be omitted if it's the last line of code with nothing else that will be executed/included thereafter.
PHP requires that you end lines with a semicolon unless it is the last line. This is because if the semicolon is omitted, it will be evaluated as the same expression as the next line. Change this:
$uname = "Name"
To this:
$uname = "Name";
Missing a semicollen.
$uname = "name";
write it like this
just add ; at the end
$uname = "Name";
You need to end each statement with a semicolon ;
This error most often means you forgot one of these
' ; ' " ) ( [ ]
Change
$uname = "Name"
with
$uname = "Name";

Categories