How to write poll data to a text file? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to make a poll in php. Im trying to gather data by writing the info to a txt file. How do I get the code to write the data to a txt file?
This all the code I have in my handler, how do I make it write to my txt file. Most of the stuff at the bottom doesn't matter yet. Try to look at the code that say if ($submit == 'submit') and what follows that.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
$handle = fopen($filename, 'a');
//next is the stuff that is to be appended
if ($submit == 'Submit') {
fopen($filename, 'w');
$newdata = $Soda . PHP_EOL;
fwrite($handle, $newdata);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>

this should work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Poll</title>
</head>
<body>
<?php
//no need for sport validation is unimportant and doesnt work
if (isset($_REQUEST['Soda'])) {
$Soda = $_REQUEST['Soda'];
} else {
$Soda = NULL;
echo '<p class="error">You forgot to select your favorite soda!</p>';
}
//This is end of soda validation
if (!empty($_REQUEST['Book'])) {
$Book = $_REQUEST['Book'];
} else {
$Book = NULL;
echo '<p class="error">You forgot to write in your favorite book!</p>';
}
//End of book validation
if (isset($_REQUEST['SOTU'])) {
$SOTU = $_REQUEST['SOTU'];
} else {
$SOTU = NULL;
echo '<p class="error">You forgot to select the two biggest issues of the state of the union address!</p>';
}
//End of SOTU validation
if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) && isset($_REQUEST['SOTU'])) {
echo' Thank You for filling out the survey!<br> You can see the results of the pole' . " here!<br><br> Your response has been recorded.";
} else {
echo '<p class="error">Please go ' . "back" . ' and fill out the poll!<p>';
}
//End of link responses
//Define variables and make sure file works
$submit = $_REQUEST['submit'];
$filename = 'poll_data.txt';
//next is the stuff that is to be appended
if ($submit == 'Submit') {
$handle = fopen($filename, 'a');
fputs($handle, $Soda.PHP_EOL);
fclose($handle);
} else { echo 'You didn\'t click submit';}
//Now to sort the data and present it
/*explode('PHP.EOL', $filename);
$CC = 0;
$P = 0;
$MD = 0;
$SS = 0;
$BR = 0;
$DLS = 0;
$O = 0;
foreach($filename as $value) {
if ($value = 'Coca-Cola') {
$CC = $CC + 1;
}
elseif ($value = 'Pepsi') {
$P = $P + 1;
}
elseif ($value = 'MtnDew') {
$MD = $MD + 1;
}
elseif ($value ='Sprite/Sierra-Mist') {
$SS = $SS + 1;
}
elseif ('BigRed') {
$BR = $BR + 1;
}
elseif ('DontLikeSoda') {
$DLS = $DLS + 1;
}
elseif ('Other') {
$O = $O + 1;
}
}*/
?>

I think if you use
fopen($filename, 'r');
that should work.
http://www.php.net/manual/en/function.fopen.php

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

How to correctly write a foreach for $clientList in TS3AntiVPN application?

I download a TS3AntiVPN but it shoes an error. I use a Linux Server running Debian 9 Plesk installed.
PHP Warning: Invalid argument supplied for foreach() in
/var/www/vhosts/suspectgaming.de/tsweb.suspectgaming.de/antivpn/bot.php
on line 29
How do I solve this problem?
<?php
require("ts3admin.class.php");
$ignore_groups = array('1',); // now supports one input and array input
$msg_kick = "VPN";
$login_query = "serveradmin";
$pass_query = "";
$adres_ip = "94.249.254.216";
$query_port = "10011";
$port_ts = "9987";
$nom_bot = "AntiVPN";
$ts = new ts3Admin($adres_ip, $query_port);
if(!$ts->getElement('success', $ts->connect())) {
die("Anti-Proxy");
}
$ts->login($login_query, $pass_query);
$ts->selectServer($port_ts);
$ts->setName($nom_bot);
while(true) {
sleep(1);
$clientList = $ts->clientList("-ip -groups");
foreach($clientList['data'] as $val) {
$groups = explode(",", $val['client_servergroups'] );
if(is_array($ignore_groups)){
foreach($ignore_groups as $ig){
if(in_array($ig, $groups) || ($val['client_type'] == 1)) {
continue;
}
}
}else{
if(in_array($ignore_groups, $groups) || ($val['client_type'] == 1)) {
continue;
}
}
$file = file_get_contents('https://api.xdefcon.com/proxy/check/?ip='.$val['connection_client_ip'].'');
$file = json_decode($file, true);
if($file['message'] == "Proxy detected.") {
$ts->clientKick($val['clid'], "server", $msg_kick);
}
}
}
?>
You might be missing data in your first array. You may add an if statement to check if there is data in your $clientList['data'] var:
if (is_array($clientList['data'])) {
}
Or you might also check if sizeof(); of your array is larger than a number, you may desire.
if (is_array($clientList['data']) && sizeof($clientList['data']) > 0) {
}
Code
require "ts3admin.class.php";
$ignore_groups = array('1'); // now supports one input and array input
$msg_kick = "VPN";
$login_query = "serveradmin";
$pass_query = "";
$adres_ip = "94.249.254.216";
$query_port = "10011";
$port_ts = "9987";
$nom_bot = "AntiVPN";
$ts = new ts3Admin($adres_ip, $query_port);
if (!$ts->getElement('success', $ts->connect())) {
die("Anti-Proxy");
}
$ts->login($login_query, $pass_query);
$ts->selectServer($port_ts);
$ts->setName($nom_bot);
while (true) {
sleep(1);
$clientList = $ts->clientList("-ip -groups");
if (is_array($clientList['data']) && sizeof($clientList['data']) > 0) {
foreach ($clientList['data'] as $val) {
$groups = explode(",", $val['client_servergroups']);
if (is_array($ignore_groups)) {
foreach ($ignore_groups as $ig) {
if (in_array($ig, $groups) || ($val['client_type'] == 1)) {
continue;
}
}
} else {
if (in_array($ignore_groups, $groups) || ($val['client_type'] == 1)) {
continue;
}
}
$file = file_get_contents('https://api.xdefcon.com/proxy/check/?ip=' . $val['connection_client_ip'] . '');
$file = json_decode($file, true);
if ($file['message'] == "Proxy detected.") {
$ts->clientKick($val['clid'], "server", $msg_kick);
}
}
} else {
echo "There might be no data in Client List";
}
}

Why is the "Learn More" link not linking to the page?

I'm trying to understand how this code (from another developer) is written. It has a bug but I can't seem to fix it. The learn more link doesn't link to the post in the custom field.
I've tried to remove the learn more lines but it then it changes the slide link to link to the image itself and not what's present in the custom link field.
$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
$items = array();
foreach ($slides as $slide) {
//echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
$item = new stdClass();
if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
if (isset($slide->custom_data['ons_slide_image'])) {
$item->src = $slide->custom_data['ons_slide_image'];
}
if (isset($slide->custom_data['ons_slide_heading'])) {
$item->heading = $slide->custom_data['ons_slide_heading'];
$item->heading .= '<span class="punctuation">.</span><span class="learn_more"> »</span>';
}
if (isset($slide->custom_data['ons_slide_caption'])) {
$item->caption = $slide->custom_data['ons_slide_caption'];
$item->caption .= ' Learn more »';
}
if (isset($slide->custom_data['ons_slide_href'])) {
$item->href = $slide->custom_data['ons_slide_href'];
} else {
$item->href = "#";
}
}
$items[] = $item;
}
$carousel = new ONS_Bootstrap_Carousel($items);
echo $carousel;
}
You are already doing something with $slide->custom_data['ons_slide_href']; but AFTER the lines of code that are outputting your anchor tag.
So try switching the processing about a bit like this
$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
$items = array();
foreach ($slides as $slide) {
//echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
$item = new stdClass();
if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
if (isset($slide->custom_data['ons_slide_image'])) {
$item->src = $slide->custom_data['ons_slide_image'];
}
if (isset($slide->custom_data['ons_slide_heading'])) {
$item->heading = $slide->custom_data['ons_slide_heading'];
$item->heading .= '<span class="punctuation">.</span><span class="learn_more"> »</span>';
}
// moved this code above the anchor tag line
if (isset($slide->custom_data['ons_slide_href'])) {
$item->href = $slide->custom_data['ons_slide_href'];
} else {
$item->href = "#";
}
// Now concatenate $item->href in the anchor tag line
if (isset($slide->custom_data['ons_slide_caption'])) {
$item->caption = $slide->custom_data['ons_slide_caption'];
$item->caption .= ' Learn more »';
}
}
$items[] = $item;
}
$carousel = new ONS_Bootstrap_Carousel($items);
echo $carousel;
}

PHP PDO Pagination not refreshed

im quite new to PHP and im trying to make a pagination, the code works! but i need to click on the button twice to make the page refreshed/change. how do i fix this?
view/main/user/userlist.php
<?php
$_SESSION['pagelim'] = 10;
$_SESSION['page'] = $_GET['halaman'];
if ($_SESSION['page'] == '') {
$_SESSION['pos'] = 0;
$_SESSION['page'] = 1;
} else {
$_SESSION['pos'] = ($_SESSION['page']- 1) * $_SESSION['pagelim'];
}
$itemcount = listpetugas::getlisted();
$pagecount = ceil(listpetugas::getlisted()/$_SESSION['pagelim']);
for ($i=1;$i<=$pagecount;$i++)
{ ?>
<?php
if ($i!=$pagecount){
echo " <ul class='pagination'><li><a href='?controller=main&action=userlist&halaman=$i' onclick='myFunction()'>$i</a></li></ul>";?>
<?php }
else{
echo "$i";
} ?>
<?php } ?>
model/userdb.php
public static function listemup()
{
if ($_SESSION['pos'] =='' && $_SESSION['pagelim'])
{
$pos = 0;
$lim = 10;
}
else {
$pos = $_SESSION['pos'];
$lim = $_SESSION['pagelim'];
}
$list = [];
$db = FirstFire::StartConnection();
$req = $db->query("SELECT * FROM randomity LIMIT $pos,$lim");
$rowcount = $req->rowCount();
foreach ($req->fetchAll() as $post) {
$list[] = new listpetugas($post['name'],$post['email'],$post['adress'],$post['wow']);
}
return $list;
}
JS
<script>
function myFunction() {
location.reload();
}
</script>
sorry for the messy code.

base64 in PHP Otp Library

I trying to make some simple library for encrypting files in PHP with OTP method. My problem is that some chars in decrypted code are different than original. I worked on it almost one week but without result. Is there problem with base64 chars or with encoding/decoding mechanism ?
Many thanks for the answers.
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
The problem seems to be only happening when $poziciaaktualneho is equal to $poziciasifrovana and so by adding another if statement on line 78 to check for this and instead set $move equal to $poziciasifrovana I was able to fix the problem. The below script should work:
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if ($poziciasifrovana == $poziciaaktualneho) {
$move = $poziciasifrovana;
} elseif($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
Warning: I would not recommend using my solution in an enterprise setting if at all since I do not know why this fixes your script or what was
originally wrong with it and it is most likely not air tight.

Categories