I have a function in php, which lists followers of a user in twitter. When I use chrome to navigate to the page which holds the function, It works, sometimes it doesn't.
Chrome works 3 out of 5 times before it gives "Error 330 (net::ERR_CONTENT_DECODING_FAILED): Unknown error"
Mozilla works 4 out of 5 times before it gives "Content Encoding Error"
Opera shows this
Warning: Invalid argument supplied for foreach() in
C:\wamp\www\ntwyt\core\common\twitter.php on line 1639
�j&x���o��4�a ���h��RA�N����T.&�Km�n� 5ý i�<Т
K���/o4�¨�b�#�Y�����
Here's the code
function theme_followers($feed, $hide_pagination = false) {
$rows = array();
if (count($feed) == 0 || $feed == '[]') return '<p>No users to display.</p>';
foreach ($feed->users->user as $user) {
$name = theme('full_name', $user);
$tweets_per_day = twitter_tweets_per_day($user);
$last_tweet = strtotime($user->status->created_at);
$content = "{$name}<br /><span class='about'>";
if($user->description != "")
$content .= "Bio: " . twitter_parse_tags($user->description) . "<br />";
if($user->location != "")
$content .= "Location: {$user->location}<br />";
$content .= "Info: ";
$content .= pluralise('tweet', $user->statuses_count, true) . ", ";
$content .= pluralise('friend', $user->friends_count, true) . ", ";
$content .= pluralise('follower', $user->followers_count, true) . ", ";
$content .= "~" . pluralise('tweet', $tweets_per_day, true) . " per day<br />";
$content .= "Last tweet: ";
if($user->protected == 'true' && $last_tweet == 0)
$content .= "Private";
else if($last_tweet == 0)
$content .= "Never tweeted";
else
$content .= twitter_date('l jS F Y', $last_tweet);
$content .= "</span>";
$rows[] = array('data' => array(array('data' => theme('avatar', theme_get_avatar($user)), 'class' => 'avatar'),
array('data' => $content, 'class' => 'status shift')),
'class' => 'tweet');
}
$content = theme('table', array(), $rows, array('class' => 'followers'));
if (!$hide_pagination)
$content .= theme('list_pagination', $feed);
return $content;
}
Whats the problem? It works and doesn't work.
I think you want to loop:
foreach ($feed->users as $user) {
....
}
Related
I have a multistep form (HTML / JS / PHP) and the form data gets sent to a backend after submitting, which results in a page reload.
Is it possible to pass the data live to the backend when the user gets to a new page of the form (clicks on the next button)?
<?php
require_once dirname(__FILE__) . '/MailWizzApi/Autoloader.php';
MailWizzApi_Autoloader::register();
// configuration object
$config = new MailWizzApi_Config(array(
'apiUrl' => 'http://email.mddasd.de/api/index.php',
'publicKey' => 'SAMPLEKEY',
'privateKey' => 'SAMPLEKEY',
// components
'components' => array(
'cache' => array(
'class' => 'MailWizzApi_Cache_File',
'filesPath' => dirname(__FILE__) . '/MailWizzApi/Cache/data/cache', // make sure it is writable by webserver
)
),
));
// now inject the configuration and we are ready to make api calls
MailWizzApi_Base::setConfig($config);
// start UTC
date_default_timezone_set('UTC');
$email = $_POST ['email'];
$name = $_POST['name'];
$phone = $_POST['telephone'];
if (isset($_POST['branch_0_answers']) && $_POST['branch_0_answers'] != "")
{
foreach($_POST['branch_0_answers'] as $value)
{
$art.= trim(stripslashes($value)) . "\n";
};
$grundflaeche .= $_POST['grundflaeche-grundstueck'] . "\n";
$plz .= $_POST['plz'] . "\n";
}
if (isset($_POST['branch_1_answers']) && $_POST['branch_1_answers'] != "")
{
foreach($_POST['branch_1_answers'] as $value)
{
$art.= trim(stripslashes($value)) . "\n";
};
$wohnflaeche .= $_POST['wohnflaeche-haus'] . "\n";
$grundflaeche .= $_POST['grundflaeche-haus'] . "\n";
$baujahr .= $_POST['baujahr'] . "\n";
$plz .= $_POST['plz'] . "\n";
}
if (isset($_POST['branch_2_answers']) && $_POST['branch_2_answers'] != "")
{
foreach($_POST['branch_2_answers'] as $value)
{
$art.= trim(stripslashes($value)) . "\n";
};
$wohnflaeche .= $_POST['wohnflaeche-wohnung'] . "\n";
$baujahr .= $_POST['baujahr'] . "\n";
$plz .= $_POST['plz'] . "\n";
}
if (isset($_POST['branch_3_answers']) && $_POST['branch_3_answers'] != "")
{
foreach($_POST['branch_3_answers'] as $value)
{
$art.= trim(stripslashes($value)) . "\n";
};
$gewerbeflaeche .= $_POST['gewerbeflaeche'] . "\n";
$grundflaeche .= $_POST['grundflaeche-gewerbe'] . "\n";
$baujahr .= $_POST['baujahr'] . "\n";
$plz .= $_POST['plz'] . "\n";
}
foreach($_POST['branch_1_1_answers'] as $value)
{
$stand.= trim(stripslashes($value)) . "\n";
};
$endpoint = new MailWizzApi_Endpoint_ListSubscribers();
$response = $endpoint->create('ma503j6m97b75', array(
'EMAIL' => $email,
'TELEFON' => $phone,
'NAME' => $name,
'ART' => $art,
'GEWERBEFLAECHE' => $gewerbeflaeche,
'WOHNFLAECHE' => $wohnflaeche,
'GRUNDFLAECHE' => $grundflaeche,
'BAUJAHR' => $baujahr,
'PLZ' => $plz,
'STAND' => $stand
));
?>
The .php gets executed with the final submit button.
Thanks for your answers.
I'm a complete newb in PHP, and the only reason I got this far was because I was trying to guess what everything does. I guess I was lucky that it worked this far.
So basically, I'm using the PHP Wake On LAN code that I found here to make a simple page that I log into, to remotely turn on my computer. I decided to add a username, rather than just a password, so maybe I could have multiple users. I've done everything successfully so far except one thing. When I log in, check my computer, and hit "Wake all selected", it logs me out immediately and doesn't send the magic packet. Any tips on what I could change to fix this? As soon as I remove the 'and' and the username bit in the if statement that checks for both user and pass to be correct, it works. But then there's no user and pass verifying going on. The if statement I'm talking about is the second if under the === Test for password protection === part.
You can demo the code on my site to see the issue in action here: http://trivisionzero.com/wol/
Just use 'user', 'pass'. (to recreate it, select any computer and press wake button)
Full code so far:
<center>
<br><br><br><br><br>
<?php
/* ============================== Configuration settings ====================================== */
/* List of PCs that may be woken */
$config_network_data_array[] = array("name" => "Shane-EPC", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");
$config_network_data_array[] = array("name" => "Demo", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");
$config_network_data_array[] = array("name" => "Demo", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");
// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "7";
$my_password = 'pass';
$my_username = 'user';
$html_title = '<H2>TrivisionZero PC Waker</H2>';
$config_table_columns = array('name', 'IP', 'MAC', 'links');
# The following function is copied (with some edits, to suppress output and return TRUE or an error message) from:
# http://www.hackernotcracker.com/2006-04/wol-wake-on-lan-tutorial-with-bonus-php-script.html
# Wake on LAN - (c) HotKey#spr.at, upgraded by Murzik
# Modified by Allan Barizo http://www.hackernotcracker.com
flush();
function WakeOnLan($addr, $mac,$socket_number) {
$separator = ':';
if (strstr ( $mac, '-' ) ) {
$separator = '-';
}
$addr_byte = explode($separator, $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
// echo "Error creating socket!\n";
// echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
return "Error creating socket!\nError code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_erro
($s));
// return FALSE;
}
else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret <0) {
// echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
// return FALSE;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
// echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
}
else {
// echo "Magic packet failed!";
return "Magic packet failed!";
// return FALSE;
}
}
}
/* ============================== some predefined texts ====================================== */
$display_sent = 'Magic Packet sent successfully!';
$button_text = 'Wake!';
$button_text2 = 'Wake all selected';
//this is where I added my username part
$username_element = "<P>Username: <input type=\"text\" name=\"username\" />";
$password_element = "<P>Password: <input type=\"password\" name=\"password\" /><input type=\"submit\" name=\"submit\" value = \"Login\" />";
$table_html = "<TABLE border=\"2\">\n";
$logout_html = '';
/* ========================= Test for password protection ==================================== */
$wake_MAC_array = array();
if (!isset ($_POST['logout'])) {
$input_password = $_POST['password'];
$input_username = $_POST['username'];
}
//($my_password === '') is if you want no password
if (($input_password === $my_password) and ($input_username === $my_username)) {
$logged_in = TRUE;
$hidden_login = "<input type=\"hidden\" name=\"password\" value=\"$my_password\"/>";
if ($my_password !== '') {
$logout_html = "\n<P><input type=\"submit\" name=\"logout\" value=\"Log Out\"/>\n";
}
if ( (isset ($_POST['tickbox'])) and (is_array($_POST['tickbox']) ) ) {
$checkbox_array = $_POST['tickbox'];
foreach ($checkbox_array as $mac_address => $tickbox_setting) {
$wake_MAC_array[$mac_address] = $tickbox_setting;
}
}
} else {
$logged_in = FALSE;
$hidden_login = '';
$table_html_user = $username_element;
$table_html = $password_element;
}
/* ================================ LOGGED-IN users only ===================================== */
/* ======================= construct table for listing of devices ============================ */
if ($logged_in == TRUE) {
$table_row = "\n<TR>";
foreach ($config_table_columns as $key => $column_heading) {
$table_row .= '<TD>' . $column_heading . '</TD>';
}
$table_row .= '<TD>Wake Up!</TD>';
$table_row .= '<TD>status</TD>';
$table_html .= $table_row . "</TR>\n";
foreach ($config_network_data_array as $device_key => $device_values) {
$table_row = "\n<TR>";
$mac = $device_values['MAC'];
$device_name = $device_values['name'];
$status_cell = '<TD> </TD>';
foreach ($config_table_columns as $key => $column_heading) {
if (isset ( $device_values[$column_heading])) {
$value = $device_values[$column_heading];
if ($column_heading == 'MAC') {
/* special coding for MAC address column; prepare clickable button */
$this_MAC = $value;
$value = "<input type=\"submit\" name=\"wake_MAC\" value = \"$value\" />";
if (( $_POST['wake_MAC'] === $this_MAC ) or (array_key_exists ($this_MAC,
$wake_MAC_array))) {
$status = WakeOnLan ($device_values['WakeIP'], $this_MAC, $socket_number) ;
if ( $status === TRUE ) {
$status = $display_sent;
}
$status_cell = "<TD>$status</TD>";
}
}
} elseif ($column_heading == 'links') {
/* special coding for links column; prepare clickable links from $config_network_links_array */
$value = '';
if (isset ( $config_network_links_array[$device_name])) {
foreach ($config_network_links_array[$device_name] as $link_title => $link_URL) {
if ( $value !== '') {
$value .= '<BR />';
}
$value .= '' . $link_title . '';
}
}
} else {
$value = '';
}
if ($value === '') {
$value = ' ';
}
$table_row .= '<TD>' . $value . '</TD>';
}
/* now add a checkbox to wake up this device */
$table_row .= '<TD>' . "<input type=\"checkbox\" name=\"tickbox[$this_MAC]\" />" . '</TD>';
/* now add the status message (if applicable) for the attempt to send a packet to this device */
$table_row .= $status_cell;
$table_html .= $table_row . "</TR>\n";
}
$table_html .= "</TABLE>\n";
$table_html .= "<P><input type=\"submit\" name=\"wake all\" value = \"$button_text2\" />\n";
}
/* =========================================================================================== */
/* ======================= Now output the html that we've built ============================== */
echo $html_title;
echo "<FORM name=\"input\" action=\"" .$_SERVER['PHP_SELF'] . "\" method=\"post\">";
echo '<P>';
echo $table_html_user;
echo $table_html;
echo $hidden_login;
echo $logout_html;
echo "</FORM>\n";
?>
</center>
When you are logged in and then submit the form to wake a machine, you are passing the password in a hidden field, but not the username.
As you are not passing the username, $input_username = null and thus the check if (($input_password === $my_password) and ($input_username === $my_username)) becomes if (('pass' === 'pass') and (null === 'user')) which is false and this is why you get logged out.
I have this function in $root/content/plugins/musicplayer/includes/player.php
public function head_script( $id, $playlist_id, $songs, $in_popup, $autoplay = false ) {
$output = '';
$playlist = '';
$artist = '';
$free = null;
$external = 0;
if ( $songs ) {
$ogg = '';
foreach ( $songs as $song ) {
$free = $song->free;
if ( $song->poster ) {
$poster = esc_url( $song->poster );
} else {
$poster = $this->get_default_playlist_poster( $playlist_id );
}
$playlist .= '{ title : "' . $song->name . '", mp3:"'. esc_url( $song->mp3 ) .'"';
if ( $song->artist )
$playlist .= ', artist : "' . $song->artist . '" ';
if ( $free != 'on' ) {
$playlist .= ',poster : "' . $poster . '" ';
$playlist .= ' },';
}
$playlist = substr( $playlist, 0, -1 );
$output .= '<script type="text/javascript">//<![CDATA[';
$output .= "\n";
$output .= 'jQuery(document).ready(function($) {
new jPlayerPlaylist( {
jPlayer: "#jquery_jplayer_' . $id . '",
cssSelectorAncestor: "#jp_container_' . $id . '" },
['.$playlist.'], {
swfPath: "' . WOLF_JPLAYER_PLUGIN_URL . '/assets/js/src",
wmode: "window", ';
$output .= '});'; // end playlist
if ( ! $in_popup )
$output .= $this->popup();
$output .= '});'; // end document ready playlist
$output .= '//]]></script>';
}
echo $output;
}
How can I use it in $root/content/themes/bigwolf/index.php, with it still being able to call all the functions that are originally and normally called in the native directory, without any problem?
You can include it. That's how you do it.
f1.php
<?php
function func1()
{
echo 'hi';
}
f2.php
<?php
require_once('f1.php'); // require
//include 'f1.php'; // or include
func1();
Obviously musicplayer is a plugin. If it's properly formatted, WordPress will automatically include it. So in the main file of your plugin put
require_once __DIR__ . "/includes/player.php';
That will bring your player.php in and give you access to the functions in it.
HTH,
=C=
I have this form where I wan't people to sign up for receiving free stuff campaign. The form works fine and its send the details to the requested eMail.
Now this form should also write the details to a csv file that we can handle the data more easily.
I use this form:
$value) {
${"".$key} = $value;
}
if (!isset($email)) {
header( "Location: $formurl" );
exit;
}
if ($_POST['services']) {
foreach($_POST['services'] as $value) {
$check_msg .= "- $value\n";
}
}
function is_email($email) {
return ereg("^[^# ]+#[^#]+\.[^# ]+$", $email);
}
function validate_email($email) {
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*#([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("#",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
$formurl = "/sticker/index.php" ;
$errorurl1 = "/sticker/error.php" ;
$errorurl2 = "/sticker/error.php" ;
$thankyouurl = "/sticker/success.php#start" ;
$http_referrer = getenv( "HTTP_REFERER" );
// get POST && GET vars to run with 'register_globals off'
//foreach($_GET AS $key => $value) {
// ${"".$key} = $value;
//}
$messageproper =
"============================================================\n" .
"Sticker Request $firstname $lastname\n" .
"============================================================\n\n" .
"KUNDENDATEN\n\n" .
"Vorname: $firstname\n" .
"Nachname: $lastname\n" .
"Firma: $company\n" .
"Strasse: $street\n" .
"Strasse2: $street2\n" .
"PLZ: $zip\n" .
"City: $city\n" .
"Land: $country\n\n" .
"Telefon: $phone\n" .
"eMail: $email\n" .
"Facebook Name: $fbname\n\n" .
"--------------------------------------\n\n" .
"Bemerkung: $comment\n\n" .
"============================================================\n\n" ;
$myFile = "/sticker/sticker.txt";
$fh = fopen($myFile, 'a');
$stringData = "\r\n";
$x=0;
foreach($_POST AS $key => $value) {
$x!=0?$stringData=",".$stringData:'';
$stringData='"'.$firstname.'";"'.$lastname.'";"'.$company.'";"'.$street.'";"'.$street2.'";"'.$zip.'";"'.$city.'";"'.$country.'";"'.$phone.'";"'.$email.'";"'.$fbname.'";"'.$comment.'"'."\r\n";
$x++;
}
fwrite($fh, $stringData);
fclose($fh);
$mailto = 'email#domain.com' ;
$subject = "Free Sticker Campaign: $company - $firstname $lastname";
mail($mailto, $subject, $messageproper, "From: \"$firstname $lastname\" <$email>\nReply-To: \"$lastname\" <$email>\nX-Mailer: chfeedback.php 2.01" );
header( "Location: $thankyouurl" );
exit;
?>
As I said nothing gets written to the CSV File I declared in $myFile.
Would be great if someone could let me know what to do.
Thanks.
I'm such a noob at PHP. I'm really stuck on this and could do with some help!
I'm using the following code to control if a div class and span ID appears when a variable (e.g. $project1title and $project1description) is empty/not empty.
$html = '<div class="infobar"><div class="infobar-close"></div>';
$content = false;
if ($project1title !== '' && $project1description !== '')
{
$html .= '<span id="title"></span><span id="description"></span>';
$content = true;
}
// etc.
$html .= '</div>';
if ($content)
{
echo $html;
}
The code works fine when I've only got one project title & description there but when I start adding ($project2title !== '' && $project2description !== '') if ($project3title !== '' && $project3description !== ''), etc it messes up because I need a unique ID for each project title & description. My question is, how can I make each of them have a unique ID? (Would I need to use arrays?) And once I've given each project a unique ID, where in the code above would I need to declare each unique ID?
What about this :
<?php
$projects = array(
array(
'title' => 'myTitle',
'description' => 'my description',
),
array(
'title' => 'myTitle2',
'description' => 'my description2',
),
);
$html = '<div class="infobar"><div class="infobar-close"></div>';
$content = false;
$id = 1;
foreach($projects as $project){
if(!empty($project['title']) && !empty($project['description'])){
$html .= '<span id="title'. $id .'"></span><span id="description'. $id .'"></span>';
$content = true;
}
$id++;
}
$html .= '</div>';
if ($content) {
echo $html;
}
Note that if id is important for you, you can add an id key in the projects array.
Does that help ?
Alternatively, I think you should take a look at dynamic vars :
here is simple example of what you can do with them :
<?php
$html = '<div class="infobar"><div class="infobar-close"></div>';
$content = false;
for($i = 1;$i<=10;$i++){
$title = 'project' . $i . 'title';
$description = 'project' . $i . 'description';
if (isset($$title) && isset($$description) && $$title !== '' && $$description !== '')
{
$html .= '<span id="title'. $i .'"></span><span id="description'. $i .'"></span>';
$content = true;
}
}
$html .= '</div>';
if ($content)
{
echo $html;
}
Shouldn't the operations be:
!=
instead of
!==
?