Image Code Refresh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Doom87
Forum Newbie
Posts: 3
Joined: Wed Aug 15, 2007 8:31 pm

Image Code Refresh

Post by Doom87 »

Hello. I'm new here. I have a registration page for my site, and on that page i have a image code. Everything works fine but, what I want to do is make a link or button to refresh the image in case the user can't read it without causing them to lose all of their data they have entered.

Here is a link to the page i have:
http://www.ootaclan.org/test/register.php

Here's the code to my page:

Code: Select all

<?php

session_start();
include("db.php");
include("mainfile.php");
include("securimage.php");

docstart();
dochead();

function usernameTaken($username) {
   global $conn;
   if(!get_magic_quotes_gpc()){
      $username = addslashes($username);
   }
   $q = "select username from users where username = '$username'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}

function addNewUser($username, $password, $realname, $email, $website, $icq, $aim, $yim, $msn, $location, $occupation, $interests) {
   global $conn;
   $q = "INSERT INTO users VALUES ('$username', '$password', '$realname', '$email', '$website', '$icq', '$aim', '$yim', '$msn', '$location', '$occupation', '$interests')";
   return mysql_query($q,$conn);
}

function displayStatus() {
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']) {
      echo "<font color='C0C0C0'><h1>Registered!</h1>\n";
      echo "<p>Thank You <b>" . $uname . "</b>, your information has been added to our database, you may now <a href='index.php' title='Login'>log in</a>.</font>\n";
      }
   else {
      echo "<h1>Registration Failed</h1>";
      echo "<p>We are sorry, but an error has occured and your registration for the username <b>" . $uname . "</b>, could not be completed.<br>";
      echo "Please try again at a later time.";
   }

   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])) {
   docstart();
   displayStatus();
   return;
}

if(isset($_POST['subjoin'])) {
   if(!$_POST['user'] || !$_POST['pass'] || !$_POST['confirmpass'] || !$_POST['email'] || !$_POST['confirmemail'] || !$_POST['realname']) {
      die("<font color=\"#c0c0c0\">You didn't fill in a required field.<br><a href='register.php' target='_self'>Click here to try again</a></font>");
   }

   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30) {
      die("<font color=\"#c0c0c0\">Sorry, the username is longer than 30 characters, please shorten it.<br><a href='register.php' target='_self'>Click here to try again</a></font>");
   }

   $pass1 = md5($_POST['pass']);
   $pass2 = md5($_POST['confirmpass']);
   if($pass1 != $pass2) {
      die("<font color=\"#c0c0c0\">Your Passwords do not match<br>Please try re-entering your passwords<br><a href='register.php' target='_self'>Click here to try again</a></font>");
   }

   if($_POST['email'] != $_POST['confirmemail']) {
      die("<font color=\"#c0c0c0\">Your Email Addresses do not Match<br>Please try again.<br><a href='register.php' target='_self'>Click here to try again</a></font>");
   }

   $img = new Securimage();
   $valid = $img->check($_POST['code']);
   if($valid == false) {
      die("<font color=\"#c0c0c0\">Sorry The Image Code you Entered is incorrect!<br><br><a href=\"javascript:history.go(-1)\">Please try Again</a></font>");
   }

   if(usernameTaken($_POST['user'])) {
      $use = $_POST['user'];
      die("<font color=\"#c0c0c0\">Sorry, the username: <strong>$use</strong> is already taken, please pick another one.<br><a href='register.php' target='_self'>Click here to try again</a></font>");
   }

   $md5pass = md5($_POST['pass']);
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $_POST['realname'], $_POST['email'], $_POST['website'], $_POST['icq'], $_POST['aim'], $_POST['yim'], $_POST['msn'], $_POST['location'], $_POST['occupation'], $_POST['interests']);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}

else {
?>
<div align="center">
<table border="0" width="1060" id="bodytabel" height="185" bgcolor="#000000" cellspacing="0" cellpadding="0">
   <tr>
      <td width="150" bgcolor="#212121"></td>
      <td width="5"></td>
      <td width="750"></td>
      <td width="5"></td>
      <td width="150" bgcolor="#212121"></td>
   </tr>
   <tr>
      <td width="150" bgcolor="#212121" rowspan="2" align="center" valign="top">
         &nbsp;</td>
      <td width="5">&nbsp;</td>
      <td width="750" rowspan="33"><div align="center"><font color="C0C0C0">
<h1>Register</h1></font>
<p><font color="#FF0000">* </font><font color="C0C0C0">- Indicates Required Field(s)</font></p>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
         <table align="center" border="0" cellspacing="0" cellpadding="3">
   <tr>
      <td><font color="C0C0C0">Username:</font> <font color="#FF0000">*</font></td>
      <td><input type="text" name="user" maxlength="30"></td>
   </tr>
   <tr>
      <td><font color="C0C0C0">Password:</font><font color="#FF0000"> *</font></td>
      <td><input type="password" name="pass" maxlength="30"></td>
   </tr>
   <tr>
      <td><font color="C0C0C0">Confirm Password:</font> <font color="#FF0000">*</font></td>
      <td><input type="password" name="confirmpass" maxlength="30"></td>
   </tr>
   <tr>
      <td><font color="C0C0C0">Email:</font> <font color="#FF0000">*</font></td>
      <td><input type="text" name="email" maxlength="250"></td>
   </tr>
   <tr>
      <td><font color="C0C0C0">Confirm Email:</font> <font color="#FF0000">*</font></td>
      <td><input type="text" name="confirmemail" maxlength="250"></td>
   </tr>
   <tr>
      <td><font color="C0C0C0">Real Name:</font> <font color="#FF0000">*</font></td>
      <td><input type="text" name="realname" maxlength="55"></td>
   </tr>
   <tr>
      <td colspan="2">&nbsp;</td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">Website:</font></td>
      <td><input type="text" name="website" maxlength="250"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">ICQ:</font></td>
      <td><input type="text" name="icq" maxlength="15"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">AIM:</font></td>
      <td><input type="text" name="aim" maxlength="18"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">YIM:</font></td>
      <td><input type="text" name="yim" maxlength="25"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">MSN:</font></td>
      <td><input type="text" name="msn" maxlength="25"></td>
   </tr>
      <tr>
      <td><font color="#C0C0C0">Location:</font></td>
      <td><input type="text" name="location" maxlength="250"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">Occupation:</font></td>
      <td><input type="text" name="occupation" maxlength="250"></td>
   </tr>
   <tr>
      <td><font color="#C0C0C0">Interests:</font></td>
      <td><input type="text" name="interests" maxlength="250"></td>
   </tr>
   <tr>
      <td colspan="2">&nbsp;</td>
   </tr>
   <tr>
      <td colspan="2"><font color="#c0c0c0">Please enter the image code shown below.</font>
		<font color="#FF0000">*</font><br>
         <font color="#c0c0c0">(Enter with no spaces. Characters are not case-sensitive)</font></td>
   </tr>
   <tr>
      <td><input type="text" name="code"></td>
      <td><img src="securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>"></td>
   </tr>
   <tr>
      <td colspan="2" align="right"><input type="submit" name="subjoin" value="Register!"></td>
   </tr>
</table>
</form></div>
</table>
</div>

<?php
}
echo "<div align='center'>";
footer();
echo "</div";
?>
</body>
</html>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Image Code Refresh

Post by superdezign »

Doom87 wrote:Hello. I'm new here. I have a registration page for my site, and on that page i have a image code. Everything works fine but, what I want to do is make a link or button to refresh the image in case the user can't read it without causing them to lose all of their data they have entered.
AJAX could be of use. You could also put it in a <iframe>, though passing data back and forth would have to be done manually. You could also, alternatively, design the Captcha to be more human-readable. :P
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Make your image like this:

Code: Select all

<img src="..." id="theimage">
And Make Javascript like this...

Code: Select all

function redraw(){
   img = document.getElementById('theimage');
   img.src = 'securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>;
}
PS: This would have to be added in a script tag not a js file, unless you have PHP do .JS files

And make a link like this

Code: Select all

<a href="javascript:void(0)" onclick="redraw();"> Refresh Image</a>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Be aware of the limitations of these approaches, however. How would you determine the text of the new image, yourself?
Post Reply