Parse Error in PhP Program [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to make a webpage that will take user input and add it to a .txt file. It is supposed to work like this webpage http://150.216.54.86:808/homework8/AirlineSurvey.html
Why am I receiving "Parse error: syntax error, unexpected ';'" on line 27?
<?php
$WaitTime = addslashes($_POST["wait_time"]);
$Friendliness = addslashes($_POST["friendliness"]); //missing );
$Space = addslashes($_POST["space"]);
$Comfort = addslashes($_POST["comfort"]); //missing $
$Cleanliness = addslashes($_POST["cleanliness"]);
$Noise = addslashes($_POST["noise"]);
if (empty($WaitTime) ||
empty($Friendliness) ||
empty($Space) ||
empty($Comfort) ||
empty($Cleanliness) ||
empty($Noise))
echo "<hr /><p>You must enter a value in each field. Click
your browser's Back button to return to the form.</p><hr />";
else {
$Entry = $WaitTime . "\n";
$Entry .= $Friendliness . "\n";
$Entry .= $Space . "\n";
$Entry .= $Comfort . "\n";
$Entry .= $Cleanliness . "\n";
$Entry .= $Noise . "\n";
$SurveyFile = fopen("survey.txt", "w"); /missing ;
if (flock($SurveyFile, LOCK_EX)) {
if (fwrite($SurveyFile, $Entry) > 0) {
echo "<p>The entry has been successfully added.</p>";
flock($SurveyFile, LOCK_UN;
fclose($SurveyFile);
else
echo "<p>The entry could not be saved!</p>";
}
} else
echo "<p>The entry could not be saved!</p>";
}
empty($Noise))
echo "<hr /><p>You must enter a value in each field. Click
your browser's Back button to return to the form.</p><hr />";
else {
$Entry = $WaitTime . "\n";
$Entry .= $Friendliness . "\n";
$Entry .= $Space . "\n";
$Entry .= $Comfort . "\n";
$Entry .= $Cleanliness . "\n";
$Entry .= $Noise . "\n";
$SurveyFile = fopen("survey.txt", "w"); //missing ;
//missing }
}
if (flock($SurveyFile, LOCK_EX)) {
if (fwrite($SurveyFile, $Entry) > 0) {
echo "<p>The entry has been successfully added.</p>";
flock($SurveyFile, LOCK_UN;
fclose($SurveyFile);
else
echo "<p>The entry could not be saved!</p>";
}
else {
echo "<p>The entry could not be saved!</p>";
}
}
?>

You've missed a close parenthesis ) at your flock($SurveyFile, LOCK_UN;, that's why you are receiving parse error.
It should be like this flock($SurveyFile, LOCK_UN);

Related

Loop "Addfriend" counting issue

I am having this below code, It is work perfectly. But am having a small issue messuring the "$username_index" count.
the main idea of my code is following:
having accounts.txt [92 lines] usernames:pass format.
having usernames.txt [99999 lines] usernames format.
It will login to account1, then it will add 95 usernames, then next account2 , then add next 95 usernames.
But some accounts are giving response "Too many add friends". In this case response, I will skip the account and go to next.
But below code will contiue to the next 95!, so its skip 95 from usernames!
I want it contiue adding where the left username from skipped account.
I want it as soon it will login to the next account AND CONTIUING ADD THE NEXT LINE OF USERNAMES! no need to jump to next 95 username to add!
Example how i want it:
login account1
add username1
add username2
ERROR APPEARS!
login account2
add username3
add username4
add username5
add username6
error appears!
login account3
add username7
add username8
etc..
Current Code:
$username_index = 0;
while(true) { // This is our while.. yes but this not for login()!
try {
$names = readFromFile("usernames.txt", 95, $username_index);
if(count($names) <= 0)
break;
sleep(1);
$fuckc = 0;
foreach($names as $name){
$ans = $API->addFriend($name);
$var_response = $ans->getMessage();
if (strpos($var_response, 'too many friends!') !== false) {
printf("[!] Too many friends!, Skipping account now.\n");
break;
}
if (strpos($var_response, 'Sorry') === false) {
$fuckc++;
printf("[+]" . "[" . date("h:i:s") . "]" . "[" . $fuckc . "] " . "response: " . $var_response . "\n");
//printf("[" . $fuckc . "] " . "response: " . $var_response . "\n");
}
//sleep(SLEEP_TIME);
}
$username_index += 95;
$API->logout();
//rotate_proxy();
$API = null;
//sleep(waiting);
//$results = $findFriends->getResults();
if (!isset($results) || count($results) == 0) {
if(!login()) die("Could not find a valid account.\n");
}
} catch(Exception $e){
echo $e->getMessage() . "\n";
if(!login()) die("Could not find a valid account.\n");
}
}
Edited again : the counter $username_index only increasse when $var_response is not "too many friends!" :
$username_index = 0;
while(true) { // This is our while.. yes but this not for login()!
try {
$names = readFromFile("usernames.txt", 95, $username_index);
if(count($names) <= 0)
break;
sleep(1);
$fuckc = 0;
foreach($names as $name){
$ans = $API->addFriend($name);
$var_response = $ans->getMessage();
if (strpos($var_response, 'too many friends!') !== false) {
printf("[!] Too many friends!, Skipping account now.\n");
break;
}
else $username_index++; //◄■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
if (strpos($var_response, 'Sorry') === false) {
$fuckc++;
printf("[+]" . "[" . date("h:i:s") . "]" . "[" . $fuckc . "] " .
"response: " . $var_response . "\n");
//printf("[" . $fuckc . "] " . "response: " . $var_response . "\n");
}
//sleep(SLEEP_TIME);
}
// $username_index += 95; //◄■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
$API->logout();
//rotate_proxy();
$API = null;
//sleep(waiting);
//$results = $findFriends->getResults();
if (!isset($results) || count($results) == 0) {
if(!login()) die("Could not find a valid account.\n");
}
} catch(Exception $e){
echo $e->getMessage() . "\n";
if(!login()) die("Could not find a valid account.\n");
}
}

How can I get the contents of a file one line at a time?

I have a project where a user uploads a photo with a name and caption, and I am having trouble with displaying the name and caption.
Right now, it's displaying the entire text file with each image, and I am having trouble fixing this.
My code so far:
<?php
$Dir = "files";
$DirEntries = scandir($Dir);
foreach ($DirEntries as $Entry)
{
if((strcmp($Entry, '.') != 0) && (strcmp($Entry, '..') != 0))
{
echo "<p>Name: " . file_get_contents("imagelist.txt") . "</p>";
echo "<a href=\"files/" . $Entry . "\" target=\"_blank\" >" . $Entry . "</a><br />\n";
}
}
closedir($DirOpen);
?>
Any help would be greatly appreciated.
You can use fgets():
$inputFile = fopen("file.txt", "r");
if ($inputFile) {
while (($line = fgets($inputFile)) !== false) {
echo $line."<br>";
// The process read the file line by line
}
} else {
echo "There was an error in the opening file";
}
fclose($inputFile);

Php Code (Get/Fetch Kongregate Games)

Can you help me there is a problem that is detect by php when execute but I think my code is okay.
The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"
Website of kongregate xml: here
This is the code I have:
<?php
$xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
$json = json_encode($xml);
$games = json_decode($json);
$count = 0;
foreach ($games->game as $game) {
echo $game->id . "\n";
echo $game->title . "\n";
echo $game->thumbnail . "\n";
echo $game->launch_date . "\n";
echo $game->category . "\n";
if (array_key_exists('screenshot',$game)) {
for($i=0;$i<=(count($game->screenshot)-1);$i++) {
echo $game->screenshot[$i] . "\n";
}
}
echo $game->flash_file . "\n";
echo $game->width . "\n";
echo $game->height . "\n";
echo $game->description . "\n";
echo $game->instructions . "\n";
echo $game->gameplays . "\n";
echo $game->rating . "\n\n------\n\n";
if ($count == 100) {break;}
$count++;
}
?>
You are retreiving xml.
Why are you parsing it as json?
Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.
$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);
Here: :)
$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);
$count = 0;
foreach ($xml as $game) {
echo strval($game->id) . PHP_EOL;
echo strval($game->title) . PHP_EOL;
echo strval($game->thumbnail) . PHP_EOL;
echo strval($game->launch_date) . PHP_EOL;
echo strval($game->category) . PHP_EOL;
if (isset($game->screenshot) && is_array($game->screenshot)) {
foreach ($game->screenshot as $screenshot) {
echo strval($screenshot) . "\n";
}
}
echo strval($game->flash_file) . PHP_EOL;
echo strval($game->width) . PHP_EOL;
echo strval($game->height) . PHP_EOL;
echo strval($game->description) . PHP_EOL;
echo strval($game->instructions) . PHP_EOL;
echo strval($game->gameplays) . PHP_EOL;
echo strval($game->rating) . PHP_EOL;
if ($count >= 100) {
break;
}
$count++;
}

Prepared PHP statement not printing out

I have a statement that is grabbing information from the database, and then is printed out after it is fully prepared.. For some reason though, my script is not printing out the information. I have it in this if statement:
if($community == ''){ print $community . "\n\n" . "END" . "\n"; } else { print $community; echo "hi";}
This prints out when it is ran:
() wrote:
But that is all it prints out. That is coming from the 8th $community .= line. So, my question is, why is it ONLY printing out () Wrote: and not all the variables as well?
// and ticker_symbol ='".$sym."'
$c_sql = "SELECT message_id, subject, author, FROM_UNIXTIME(datestamp,'%m-%d-%Y') AS formatted_datestamp, forum_id, body, thread, user_id FROM phorum_messages WHERE user_id=13423720 ORDER BY datestamp DESC LIMIT 5";
$c_result = mysql_query($c_sql,$connection) or die("Couldn't execute get query");
// Declare Variables
$body = $c_result['body'];
$forum_id = $c_result['forum_id'];
$user_id = $c_result['user_id'];
$author = $c_result['author'];
$formatted_datestamp = $c_result['formatted_datestamp'];
// Prepare the statement
if ($c_result != "") {
$community .= $forumPost = '<<<ENDL '. "\n";
$community .= $body . "\n";
$community .= 'ENDL;' . "\n";
$community .= '$forumPost = stripBBCode(strip_tags($forumPost));' . "\n";
$community .= "\n";
$community .= '<div class="comment">' . "\n";
$community .= '<table cellspacing="0" cellpadding="0" border="0" class="reply"><tbody><tr>' . "\n";
$community .= '<td width="90%"><b>'.$author.' ('.$formatted_datestamp.') wrote:</b><br />' . "\n";
$community .= '<p>'.iconv("ISO-8859-1//TRANSLIT", "UTF-8", $forumPost).'</p></td>' . "\n";
$community .= '</tr></tbody></table>'. "\n";
$community .= '</div>' . "\n";
}
// Print out the prepared statement
if($community = ''){ print $community . "\n\n" . "END" . "\n"; } else { print $community;}
When you are calling if($community = ''){ you only have one equals sign which will set $community to a blank string.
I think what you mean to do is if($community == ''){
It should have the double-equal:
if($community == '')
With a single = sign you're simply assigning an empty string to variable $community - and then checking whether it's true. Empty strings evaluate to false, hence you're getting into your else part - and losing your value in the process.
You only have one = sign
you need:
if($community == '') { etc...

Faster Rackspace cloud upload

I have used to the Rackspace API to upload files to the RackSpace cloud. But this method seems to be a little on the slow side. Is there a better or faster way to upload a file to the cloud(curl, http adapters, etc)?
I am currently uploading with PHP and using the provided API.
Here is my solution how to make it fast:
I'm uploading only missing files using simple PHP script below. Thanks to it I do it in just one click and in just a few seconds.
PHP source code:
function UploadMissingFilesToRackFileCDN($file_paths_to_upload, $b_force_upload = false)
{
include_once("cloudfiles.php");
// Connect to Rackspace
$username = cloudfile_username; // username
echo "Connecting to CDN..." . date("H:i:s") . "<br>"; ob_flush();
$key = cloudfile_api_key; // api key
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
echo " Connected!" . date("H:i:s") . "<br>"; ob_flush();
// Get the container we want to use
$container_name = 'vladonai';//'test_container';
echo "Obtaining container $container_name..." . date("H:i:s") . "<br>"; ob_flush();
$container = $conn->get_container($container_name);
echo " The container is obtained." . date("H:i:s") . "<br>"; ob_flush();
if (!$b_force_upload)
{
echo "Receiving container objects list..." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names = $container->list_objects();
$existing_files_count = count($existing_object_names);
echo " Objects list obtained: $existing_files_count." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names_text .= "\r\n";
foreach ($existing_object_names as $obj_name)
{
$existing_object_names_text .= $obj_name . "\r\n";
}
}
// upload files to Rackspace
$uploaded_file_n = 0;
$skipped_file_n = 0;
$errors_count = 0;
foreach ($file_paths_to_upload as $localfile_path => $file_info)
{
$filename = basename($localfile_path);
if (!file_exists($localfile_path))
{
echo "<font color=red>Error! File $localfile_path doesn't exists!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
} else
if (is_dir($localfile_path))
{
//simply skip it
} else
if (strpos($existing_object_names_text, "\r\n" . $filename . "\r\n") !== false)
{
//file is already uploaded to CDN (at least file name is present there). Would be good to have date/size checked, but CDN api has no such feature
//echo "<font color=gray>Skipped file $localfile_path - it already exists!</font><br>"; ob_flush();
$skipped_file_n ++;
} else
{
echo "<font color=green>Uploading file $localfile_path (file #$uploaded_file_n)..." . date("H:i:s") . "</font><br>"; ob_flush();
try
{
$object = $container->create_object($filename);
$object->load_from_filename($localfile_path);
$uploaded_file_n ++;
}
catch (Exception $e)
{
echo "<font color=red>Error! Caught exception: ", $e->getMessage(), " on uploading file <strong>$localfile_path</strong>!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
}
}
// if ($uploaded_file_n >= 10)
// break;
}
echo "Done! $uploaded_file_n files uploaded. Disconnecting :)" . date("H:i:s") . "<br>"; ob_flush();
echo "Skipped files: $skipped_file_n<br>"; ob_flush();
if ($errors_count > 0)
echo "<font color=red>Erorrs: $errors_count</font><br>"; ob_flush();
}
function UploadChangedImagesToRackFileCDN($b_force_upload = false)
{
$exclude = array
(
'.',
'..',
'*.html',
'*.htm',
'*.php',
'*.csv',
'*.log',
'*.txt',
'*.cfg',
//'*sub/forum/files/*',
);
$files_array_images = get_dirlist("/var/www/html/vladonai.com/images/", '*', $exclude, false);
$files_array = array_merge(get_dirlist("/var/www/html/vladonai.com/js/", '*', $exclude, false), $files_array_images);
UploadMissingFilesToRackFileCDN($files_array, $b_force_upload);
}
function get_dirlist($path, $match = '*', $exclude = array( '.', '..' ), $b_short_path = true)
{
$result = array();
if (($handle = opendir($path)))
{
while (false !== ($fname = readdir($handle)))
{
$skip = false;
if (!empty($exclude))
{
if (!is_array($exclude))
{
$skip = fnmatch($exclude, $fname) || fnmatch($exclude, $path . $fname);
} else
{
foreach ($exclude as $ex)
{
if (fnmatch($ex, $fname) || fnmatch($ex, $path . $fname))
$skip = true;
}
}
}
if (!$skip && (empty($match) || fnmatch($match, $fname)))
{
$file_full_path_and_name = $path . $fname;
//echo "$file_full_path_and_name<br>";
$b_dir = is_dir($file_full_path_and_name);
$b_link = is_link($file_full_path_and_name);
$file_size = ($b_dir || $b_link) ? 0 : filesize($file_full_path_and_name);
$file_mod_time = ($b_dir || $b_link) ? 0 : filemtime($file_full_path_and_name);
$new_result_element = array();
if ($b_short_path)
$file_name = str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name);//'[' . str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name) . ']';
else
$file_name = $file_full_path_and_name;
$result[$file_name] = array();
$result[$file_name]['size'] = $file_size;
$result[$file_name]['modtime'] = $file_mod_time;
if ($b_dir && !$b_link)
{
//recursively enumerate files in sub-directories
$result = array_merge(get_dirlist($file_full_path_and_name . "/", $match, $exclude, $b_short_path), $result);
}
}
}
closedir($handle);
}
return $result;
}

Categories