Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, 5 March 2012

Pulling in YouTube Videos from Specific Channel

class ChannelFeed {

function __construct($username){
$this->username=$username;
echo $this->username;
$this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$username.'/uploads?orderby=updated';
$this->feed=simplexml_load_file($url);
}

public function getYTid() {

$ytURL = $this->feed->entry->link['href'];

$ytvIDlen = 11; // This is the length of YouTube's video IDs

// The ID string starts after "v=", which is usually right after
// "youtube.com/watch?" in the URL
$idStarts = strpos($ytURL, "?v=");

// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
// bases covered), it will be after an "&":
if($idStarts === FALSE)
$idStarts = strpos($ytURL, "&v=");
// If still FALSE, URL doesn't have a vid ID
if($idStarts === FALSE)
die("YouTube video ID not found. Please double-check your URL.");

// Offset the start location to match the beginning of the ID string
$idStarts +=3;

// Get the ID string and return it
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
}

public function showFullFeed(){
$vidarray = array();
foreach($this->feed->entry as $video){
$vidarray[] = $video->link['href'];
}
return $vidarray ;
}


};
$youtube = new ChannelFeed('channel_name');
$vids = $youtube->showFullFeed();
$vidIDs = array_map(array(&$youtube, "getYTid"), $vids);

Friday, 17 February 2012

PHP Contact form with image verification

This is how the form will look like, this is just an image, you can download the finished files at the end of this page.

contact form preview

If you use a normal contact form then spammers will find a way to automatically send junk messages through that form, so you need a way to verify that the sender is an actual person and not an automated script.

A good solution for this is to use an random code that is generated by a script the code will be displayed as an image so only a person could read the image and type the number. This practice is called Captcha image validation. The image also has a noise added to avoid any existent automated scripts that could read the text from the image.

The contact form also makes sure the user filled all the form fields correctly, when all fields are completed and the text entered inside email box is an email address.

The contact form contents


The form will contain a link to an image, the link to image is actually a link to a php file generating the image, user will have to fill the image code inside a form field once with the other message contents (subject, sender email, etc) then click submit, if the code in the image will not be correct then message is not sent and user is asked to fill the image code again, the other form fields remain filled.
<form action="mailer.php" method="post" name="form1" id="form1" style="margin:0px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px; width:300px;" onsubmit="MM_validateForm('from','','RisEmail','subject','','R','verif_box','','R','message','','R');return document.MM_returnValue">

Your e-mail:<br />
<input name="from" type="text" id="from" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;" value="<?php echo $_GET['from'];?>"/>
<br />
<br />

Subject:<br />
<input name="subject" type="text" id="subject" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;" value="<?php echo $_GET['subject'];?>"/>
<br />
<br />

Type verification image:<br />
<input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"/>
<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br />
<br />

<!-- if the variable "wrong_code" is sent from previous page then display the error field -->
<?php if(isset($_GET['wrong_code'])){?>
<div style="border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;">Wrong verification code</div><br />
<?php ;}?>

Message:<br />
<textarea name="message" cols="6" rows="5" id="message" style="padding:2px; border:1px solid #CCCCCC; width:300px; height:100px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"><?php echo $_GET['message'];?></textarea>
<noscript><a href="http://www.thewebhelp.com" style="display:none;">contact form by thewebhelp</a></noscript>
<input name="Submit" type="submit" style="margin-top:10px; display:block; border:1px solid #000000; width:100px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;" value="Send Message"/>
</form>

Contents of the image verification script




<?php
// -----------------------------------------
// The Web Help .com
// -----------------------------------------

header('Content-type: image/jpeg');

$width = 50;
$height = 24;

$my_image = imagecreatetruecolor($width, $height);

imagefill($my_image, 0, 0, 0xFFFFFF);

// add noise
for ($c = 0; $c < 40; $c++){
$x = rand(0,$width-1);
$y = rand(0,$height-1);
imagesetpixel($my_image, $x, $y, 0x000000);
}

$x = rand(1,10);
$y = rand(1,10);

$rand_string = rand(1000,9999);
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000);

setcookie('tntcon',(md5($rand_string).'a4xn'));

imagejpeg($my_image);
imagedestroy($my_image);
?>



The php code that sends the message


When form is submitted the data is send to this script that verifies the image code and the email is sent or the user is sent back to fill the form again.


<?php
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace you@email.com with your own email address lower in this code.

// load the variables form address bar
$subject = $_POST["subject"];
$message = $_POST["message"];
$from = $_POST["from"];
$verif_box = $_POST["verif_box"];

// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("you@email.com", 'TheWebHelp.com Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
} else {
echo "no variables received, this page cannot be accessed directly";
exit;
}
?>



 

Monday, 19 December 2011

Why I Can not login via wp-admin but same time I can login from WP-login.php

I know how it feel, like no one know the solution. You like to pay everything you have, to find out why you can not login into your wordpress from wp-admin. And if you do it will redirect to same page without showing any message. but same time you can login into your wordpress from wp-login.php.  Sometime clearing the browser’s cookies might let us login again but to be honest it’s a pain, and clearing your cookies isn’t a permanent fix and do not work everyone.

Answer is easy when you know the following two solutions.

Solution1:  At   WordPress admin–> Settings–> General Settings:

“WordPress address (URL)”  should be same as “Blog address (URL)”

Solution2:  Fixing  issue with cookies.

problem is that occasionally on our various WordPress sites the wp-admin.php URL login stops working and we can only login through wp-login.php.

What we need to do is to force WordPress to follow the correct cookie path. This can be done by adding a small line of code to the wp-config.php file located in your WordPress installation root directory.

Open up wp-config.php in your web editor and add this:

/** wp-admin login fix */
@define('ADMIN_COOKIE_PATH', '/');


If you’re unsure, it can go just before the ?> at the end of the php code like so:

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
/** wp-admin login fix */
@define('ADMIN_COOKIE_PATH', '/');
?>


Update:  Some reported WP Security Scan case this issue. Please test and confirm in comments if you are using WP Security Scan wordpress plugin.

Alternative Cron


Use this, for example, if scheduled posts are not getting published.
"this alternate method uses a redirection approach,
which makes the users browser get a redirect when the cron needs to run,
so that they come back to the site immediately while cron
continues to run in the connection they just dropped.
This method is a bit iffy sometimes, which is why it's not the default."

define('ALTERNATE_WP_CRON', true);

define('DISABLE_WP_CRON', true);

Wednesday, 20 April 2011

Change of Apache port in xamp http.config file

Change apache port in xamp http.config file
C://xamp/apache/config/--> change localhost 80 to 8080

Email contact form in php

<html>
<head>
<script src="calendar.js"></script>
<style>
input {border:1px solid #ABABAB}
</style>
</head>
<body>

<?php

$t=$_GET['to'];
if ($t=="m")
//if "email" is filled out, send email
{
$t="to@mail.com";
}
else
//if "email" is not filled out, display the form
{
$t="your@mail.com";
}

?>

<form method='post' action='mailform.php'>

<input name='to' type='hidden' value='<?php echo $t; ?>' />

Email: &nbsp;&nbsp;
<input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Date &nbsp;&nbsp; :
<input name='todate' onclick="event.cancelBubble=true;this.select();lcs(this)" onfocus="this.select();lcs(this)" type="text" value="dd/mm/yy" /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>

</body>
</html>

mailform.php

<?php

$to = $_REQUEST['to'];
$subject = "New Appointment on ".$_REQUEST['todate'];
$message = $_REQUEST['message'];
$from = $_REQUEST['email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent to the concerned doctor.";

?>