Hello,
I recently added Password Protection to this photo gallery on my site.
Everything works fine, but I've been pulling my hair out trying to figure out how to do a certain thing.
- When a user enters the wrong password, I want a small message box to appear on that page, stating that he or she "Entered The Wrong Password"
-Also, when nothing is entered: same thing, a box appears stating that they haven't entered a password yet.
Here's the code I've been tinkering with:
This is the code for the VAR file for the gallery:
<?
$fold="userGallery/";
$data=$fold."data.txt"; // file data
$maxchar=2500; // maximum characters for description
$allow_post="y"; // Allow posting from Gallery Page
$adm_user="usernamehere"; // ADMIN USER
$adm_pswd="yourpassword"; // ADMIN PASSWORD
$user_pswd="userpasswordhere"; // USER PASSWORD
function conv_asc2html($str,$mode){
if ($mode==1){
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace(""",'"',$str);
}
if ($mode==2){
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace('"',""",$str);
}
$str=ltrim($str);
$str=rtrim($str);
return $str;
}
function splitwordx(&$text,$w_max,$w_cut){
if (!strstr($text," ")){ $text=$text." "; }
$word = explode(" ", $text);
if (empty($w_max)){ $w_max=10; }
if (empty($w_cut)){ $w_cut=10; }
for ($w=0; $w<count($word); $w++){
$wordlen=strlen($word[$w]);
if ($wordlen>$w_max){
$wordnew="";
$wordpart="";
for ($l=0; $l<ceil($wordlen/$w_cut); $l++){
$wordpart[$l]=substr($word[$w],($l*$w_cut),$w_cut)." ";
}
$wordnew=implode(" ",$wordpart);
$word[$w]=$wordnew;
}
}
$text = implode(" ", $word);
return $text;
}
function disperr($str){
echo "<table border=1 cellspacing=0 cellpadding=10 style=\"border-collapse:collapse\" bordercolor=#FFCCCC bgcolor=#4F6D92><tr><td><font color=#FF0000 style=\"font-size:14px\"><b>Hey You:<br>$str</td></tr></table>\n ";
}
?>
And here is the code for the Gallery:
<?
// *** LOADING CONFIG FILE
include "var.inc.php";
// *** CHECKING FILE EXISTING
if (!file_exists($data)){
disperr("File "$data" not found!");
exit;
}
?>
<html>
<head><title>Fly Society</title>
<style><!--
i {color:1B203A; font-size:10px}
font {color:FFFFFF; font-size:11px}
a {color: 4C61CE}
a:hover {color: 999FBE}
a:visited {color: 94989B}
BODY {font-family:arial; font-size:12px}
TD {font-family:arial; font-size:12px}
INPUT {font-family:arial; font-size:12px}
TEXTAREA {font-family:arial; font-size:12px}
SMALL {font-family:arial; color:FFFFFF; font-size:11px}
--></style>
</head>
<body background="gridlines.jpg" topmargin="0" leftmargin="0">
<p><img border="0" src="pdump.gif" width="482" height="163"></p>
<div align="left">
<table border="0" cellpadding="0" cellspacing="0" width="550">
<tr>
<td width="10"></td>
<td width="570"><b><font color="#FFFFFF" face="Arial Black" size="2">Scroll
to the bottom to upload any photo you wish to share with me.<br>
** AND IF I DON'T LIKE YOUR PHOTO, I'LL DELETE IT **<br><br>
<a style="font-family:Arial Black; color:999FBE" href="http://www.toofly.com/news.php">Fly Society</a> - <a style="font-family:Arial Black; color:999FBE" href="http://www.toofly.com/guestbook.php">Fly Forum</a> - <a style="font-family:Arial Black; color:999FBE" href="mailto:tonyb@toofly.com">Contact Me</a></font></b></td>
</tr>
</table>
</div><br>
<?
// *** LOADING DATA
if($file=fopen($data,"r"))
{
while(!feof($file))
{
$contents_old.=fgets($file,255);
}
fclose($file);
}
// *** (start) ALLOW POSTING
if ($allow_post=="y") {
// PASSWORD
if ($upswd==$user_pswd) {
// *** (start) POSTING NEW RECORD
if ($btpost=="Post") {
if (empty($fimage)){ $ferr.="Where's The Photo?"; }
if (empty($upswd)){ $ferr.="Please Enter The Password"; }
if (!empty($ferr)){
disperr($ferr);
} else {
$ftanggal=date("F d, Y");
splitwordx($fname,30,5);
splitwordx($fdescription,30,5);
$fdescription=substr($fdescription,0,$maxchar);
$now=time();
$desimage=$now.".jpg";
$contents_new="|line|$ftanggal";
$contents_entry="|#|$fname|#|$fdescription|#|$desimage";
$contents_new.=$contents_entry;
$contents_new=stripslashes($contents_new);
$contents_new=conv_asc2html($contents_new,1);
if (strstr($contents_old,$contents_entry)){
disperr("Duplicate entry");
} else {
if ($file=fopen($data,"w")){
$contents_new=$contents_new. "\r\n" . $contents_old;
fputs($file,$contents_new);
fclose($file);
$contents_old=$contents_new;
}
if (!empty($fimage_name)){
if (is_uploaded_file ($fimage)){
move_uploaded_file($fimage,$fold.$desimage) ;
}
}
}
}
}
}
// *** (end) POSTING NEW RECORD
?>
<script language="javascript">
<!--
function jumlahKata(form)
{ with (form)
{ sisa.value = <?=$maxchar?>-fdescription.value.length;
if (parseInt(sisa.value)<0) { sisa.value = '0'; }
fdescription.value = fdescription.value.substr(0,<?=$maxchar?>);
}
return;
}
//-->
</script>
<?
}
// *** (end) ALLOW POSTING
echo "<div align=left>\n";
echo "<table border=0 cellpadding=0 cellspacing=0 width=660>\n";
echo "<tr>\n";
echo "<td width=12>";
echo "<td width=660>";
$contents_old=conv_asc2html($contents_old,2);
$ar_row=explode("|line|",$contents_old);
$recperpage=10;
$row_first=1;
$row_last=count($ar_row)-1;
if (empty($row_start)){ $row_start=$row_first; }
$row_end=$row_start+$recperpage-1;
if ($row_end>$row_last){ $row_end=$row_last; }
if ($row_last>$recperpage){
echo "<font>Page : </font>";
for ($p=1; $p<=ceil($row_last / $recperpage); $p++){
if ($p>1){ $start=((($p-1)*$recperpage)+1);
} else { $start=1; }
echo "<font>[</font><a href=\"$PHP_SELF?row_start=$start\">$p</a><font>]</font>\n";
}
}
echo "</td>";
echo "</td>";
echo "</tr>\n";
echo "</table>\n";
echo "</div>\n";
echo "<br>";
echo " <table bgcolor=#5C6CBD border=0 cellspacing=0 cellpadding=5 style=\"border-collapse:collapse\" bordercolor=#999999>\n";
for ($r=$row_start; $r<=$row_end; $r++){
$ar_field=explode("|#|",$ar_row[$r]);
$tanggal=$ar_field[0];
$name=$ar_field[1];
$description=$ar_field[2];
$image=$fold.$ar_field[3];
echo "<tr><td background=gridlines.jpg> <td><small>$tanggal</small><td><small>$name</small></tr>\n";
echo "<tr><td bgcolor=#999FBE background=gridlines.jpg> <td bgcolor=#999FBE colspan=2>";
$image=str_replace("\r\n","",$image);
echo "<img src=\"$image\" border=0>";
echo "</tr>\n";
echo "<tr><td bgcolor=#999FBE background=gridlines.jpg> <td bgcolor=#999FBE colspan=2>$description</tr>\n";
}
echo "</table>\n";
echo "<br>";
echo "<table bgcolor=#D5D7E0 border=0 cellspacing=0 cellpadding=5 style=\"border-collapse:collapse\" bordercolor=#999999>\n";
echo "<FORM name=pesan METHOD=\"post\" ACTION=\"".$PHP_SELF."\" enctype=\"multipart/form-data\">\n";
echo "<tr><td background=gridlines.jpg> <td>Name <td><input name=\"fname\"></tr>\n";
echo "<tr><td background=gridlines.jpg> <td>Image* <td><input type=\"file\" name=\"fimage\"></tr>\n";
echo "<tr><td background=gridlines.jpg> <td>Password* <td><input type=\"password\" name=\"upswd\"></tr>\n";
echo "<tr><td background=gridlines.jpg> <td>Description <td><textarea max=\"$machar\" name=\"fdescription\" rows=5 cols=40 ";
echo " onKeyup='jumlahKata(document.pesan);' RAP";
echo "></textarea><i><br>Max $maxchar chars : </i>";
echo "<i>Chars left : </i><input type=\"text\" size=3 name=sisa maxlength=3 value=\"$maxchar\">";
echo "</tr>\n";
echo "<tr><td background=gridlines.jpg> <td colspan=2><div>* <i>Required field</i></div><div align=center><input type=\"submit\" name=\"btpost\" value=\"Post\"> <input type=\"reset\"></div></tr>\n";
echo "</FORM>";
echo "</table>\n";
echo "<br>";
echo "<div align=left>\n";
echo "<table border=0 cellpadding=0 cellspacing=0 width=660>\n";
echo "<tr>\n";
echo "<td width=12>";
echo "<td width=660>";
$contents_old=conv_asc2html($contents_old,2);
$ar_row=explode("|line|",$contents_old);
$recperpage=10;
$row_first=1;
$row_last=count($ar_row)-1;
if (empty($row_start)){ $row_start=$row_first; }
$row_end=$row_start+$recperpage-1;
if ($row_end>$row_last){ $row_end=$row_last; }
if ($row_last>$recperpage){
echo "<font>Page : </font>";
for ($p=1; $p<=ceil($row_last / $recperpage); $p++){
if ($p>1){ $start=((($p-1)*$recperpage)+1);
} else { $start=1; }
echo "<font>[</font><a href=\"$PHP_SELF?row_start=$start\">$p</a><font>]</font> ";
}
}
echo "</td>";
echo "</td>";
echo "</tr>\n";
echo "</table>\n";
echo "</div>\n";
echo "<br>";
echo "<br>";
echo "<br>";
?>
At the moment, when a user doesn't type in a password, no photo is placed into the gallery and no 'Error message: No password' box appears.
I feel that I was close to figuring this out.