Page 1 of 2

Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 9:56 am
by MesaFloyd
Hello, I have a php login script I found on evolt. For the most part works very well. I have made several successful mods, but I have a problem I just cannot solve, been working on this one issue for 2-3 weeks, Im not a real programmer so please be gentle

The script is for registering users, not unlike this forum. The register pages has 6 fields. Three are empty for the user to fill out(name-password-email), the other 3 are autofilled(IP-chkbox-timedate). When the user fills the 3fields then hits the Join! button the 6 fields all properly register to the mysql database, but, after it is registered the screen to the user does not indicate to the user that he is now registered...i.e. "Registered! Thank you..." the program just seems to jump over that part.. the screen puts up the original 'Register' screen again with the 3 fields emptied.

IB stumpted .... any help would be very much appreciated.

Here is the code. Thanks in advance for any help.

Code: Select all

 
<?php
 
session_start();
include ("database.php");
 
/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
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);
    $num = mysql_numrows($result);
    return $num;
}
 
/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($username, $password, $pcidip, $datetime, $email, $emailchk)
{
    global $conn;
    $q = "INSERT INTO users VALUES ('$username', '$password', '$pcidip', '$datetime', '$email', '$emailchk')";
    $query = mysql_query($q, $conn);
return $query;
}
 
////////This is where my problem is <I think> ...
////////When I press Join! button, the routine adds the new user to db OK but
/////// instead of going to the "Registered! Thank you.." below it empties the 3 fields(user,pass,email) and starts again
/////// asking you to register<again>. The user gets no indication that he was registered.
 
/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus()
{
    $uname = $_SESSION['reguname'];
    if ($_SESSION['regresult'])
    {
 
?>
 
<h1>Registered!</h1>
<p>Thank you.. <br>
Your information has been added to the database, you may now <a href="index.php" title="Login">log in</a></p>
 
<?php
    }
    else
    {
?>
 
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><?php echo
$uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>
 
<?php
    }
    $_SESSION['reguname'] = NULL;
    $_SESSION['registered'] = NULL;
    $_SESSION['regresult'] = NULL;
}
 
if (isset($_SESSION['registered']))
{
    /**
     * This is the page that will be displayed after the
     * registration has been attempted.
     */
?>
 
<html>
<title>Registration Page</title>
<body>
 
<?php displayStatus(); ?>
 
</body>
</html>
 
<?php
    return;
}
 
/**
 * Determines whether or not to show to sign-up form
 * based on whether the form has been submitted, if it
 * has, check the database for consistency and create
 * the new account.
 */
if (isset($_POST['subjoin']))
{
    /* Make sure all fields were entered */
    if (!$_POST['user'] || !$_POST['pass'])
    {
        die('You didn\'t fill in a required field.');
    }
 
    /* FKadded this -- checks that email field has characters */
    if (!$_POST['email'])
    {
        die('You didn\'t fill in the email field, silly');
    }
 
 
    /* Spruce up username, check length */
    $_POST['user'] = trim($_POST['user']);
    if (strlen($_POST['user']) > 30)
    {
        die("Sorry, the username is longer than 30 characters, please shorten it.");
    }
 
    /* Check if username is already in use */
    if (usernameTaken($_POST['user']))
    {
        $use = $_POST['user'];
        die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
    }
 
    /* Add the new account to the database */
    $password = ($_POST['pass']);//FK removed md5.. was... $md5pass = md5($_POST['pass']);
    $_SESSION['reguname'] = $_POST['user'];
    $_SESSION['regresult'] = addNewUser($_POST['user'], $password, $pcidip, $datetime, $email, $emailchk);//FK-added $pcidip...$emailchk
    $_SESSION['registered'] = true;
    echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
    return;
}
else
{
    /**
     * This is the page with the sign-up form, the names
     * of the input fields are important and should not
     * be changed.
     */
?>
 
<html>
<title>Registration Page</title>
<body>
<h1>Register</h1>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td>Email Addr:</td><td><input type="text" name="email" maxlength="50"></td></tr>
<tr><td>Send Email Alerts:</td><td> <input type="checkbox" name="emailchk" value="1" checked></td></tr><br />
 
<!-- FKinsert begins here -->
<tr><td>Your IP for this visit:</td><td> <input name="pcidip" type="text" value="
<?php echo $_SERVER['REMOTE_ADDR']; ?>" size="20"></td></tr><br />
 
<tr><td>Date-Time for this visit:</td><td> <input name="datetime" type="text" value="
<?php echo date("Y.m.d - h:i:s"); ?>" size="20"></td></tr><br>
<!-- FK insert ends here -->
 
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>
 
<?php
}
?>
 
 
I have marked with /////// where I think the problem is, but any help would be greatly appreciated... pls remember I am not a 'real' programmer, but I try very hard..
Floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 10:10 am
by khushbush
Ah yes, that login script...it's quite tricky and you need to make many modifications. It's given me plenty of trouble too, but it's very well written. I'm not a real programmer either...but I can try to help you, because I got the registration process working successfully :)

You could try changing this:

Code: Select all

 
<html>
<title>Registration Page</title>
<body>
<?php displayStatus();?>
</body>
</html>
 

to this:

Code: Select all

 
<html>
<title>Registration Page</title>
<body>
<?php echo displayStatus();?>
</body></html> 

Let me know what happens...

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 10:24 am
by MesaFloyd
Hi khushbush, thanks so much for your input.

I added the 'echo' statement as suggested, but no change in the results, identical problem.

any other suggestions?
Floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 10:31 am
by bovermyer
One of these two session variables is probably not being set correctly after registration:

$_SESSION['registered']
$_SESSION['regresult']


Also, adding "echo" to that particular function call won't do anything.

That script makes me cringe. It's trying to do everything - display, data handling, and data accessing - all in one single file. I strongly suggest using an MVC architecture model (model, view, controller) instead. It's also using some very old code conventions that breed bad habits.

Have a look at CakePHP for an example of an MVC-based PHP framework. It might help you figure out what I'm talking about.

http://www.cakephp.org/

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 10:40 am
by khushbush
I divided my registration page into two...I don't know where the other half of my registration page went, but this is what my page looks like:

Code: Select all

 
if($session->logged_in){
   echo "<h1>Registered</h1>";?>
   <font face = "Arial" style ="font-size:12pt" color="#0B3A62">
   <?
   echo "<p>We're sorry <b>".$_SESSION['reguname']."</b>, but you've already registered. "
       ."<a href=\"index.php\">Main</a>.</p>"; ?>
       </font>
       <?
}
/**
 * The user has submitted the registration form and the
 * results have been processed.
 */
else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>"; ?>
      <font face = "Arial" style ="font-size:12pt" color="#0B3A62">
      <?
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"main.php\">log in</a>.</p>"; ?>
          </font>
          <?
   }
   /* Registration failed */
   else{
      echo "<h1>Registration Failed</h1>";?>
      <font face = "Arial" style ="font-size:12pt" color="#0B3A62">
      <?
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>"; ?>
          </font>
          <?
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}
 
I don't know if that will be of any help...I certainly hope so!

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 10:59 am
by MesaFloyd
Thanks khushbush

This may be out of my league, I will study it and see if I can use it.
Thks
Floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 11:03 am
by khushbush
It's quite funny, cos your registration page seems way out of my league!! :lol:

Good luck! :)

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 11:08 am
by MesaFloyd
bovermyer,
thanks for your assistance...

I tried earlier to post a response to you but it apparently did not come up

Here it is again...

---- you say---
One of these two session variables is probably not being set correctly after registration:

$_SESSION['registered']
$_SESSION['regresult']
------------
my question is, do you have a suggestion??
I looked at 'cake' but I was simply hoping to have my error pointed out so I can move on.
... any suggestions?
Floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 11:10 am
by bovermyer
CakePHP is a framework and not just an authentication protocol, so it might be outside the scope of this particular problem. I was using it as an example of MVC.

There are a number of user authentication tutorials for PHP available on the Web...I'll dig around and get a list for you.

Just out of curiosity, what kind of application are you trying to build with this, Mesa? There might be an open source solution that does what you need already.

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 11:18 am
by bovermyer

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 11:57 am
by MesaFloyd
hi bover,
for some reason, I posted another message but it did not come up.
It was to you... i'll try to reconstruct...
first thanks for the links.... I have to settle on one... while they're all probably good enought for what im trying to do, i'll just stay on my monster....
My problem is frustrating because it appears to be so simple. It was working, and for the most part it does work now, just cannot get it to tell the user he is indeed registered.. for some reason, skips over that message portion... Im not sure, but it was working until my providor upgraded their servers. I had other small problems I resolved, but this one Im not sure if it is related to the server, certainly does not appear so...
Just wish I can get this simple bug squashed.... Ive been on it for weeks now.

Thanks for your help, and if you or anyone can see what is wrong, please help if you can.
Thanks for everyones effort so far.
floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 2:04 pm
by bovermyer
Two things:

1) What's your database.php file look like?

2) Change the outer quotes on lines 117 and 123 (die('You didn't fill in a required field.'); and die('You didn't fill in the email field, silly.'); ) to double quotes, or escape the apostrophe on didn't (\' instead of ').

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 2:18 pm
by MesaFloyd
Hi Ben,
I don't think that has much to do with it... because they all work fine... meaning. I am able to store properly to the database and when the user fails to fill in the fields or does not fill in the emails field, those messages appear properly...

My only problem is that when the user fills out the form <properly> the <empty> Register page comes back up suggesting he did not register, when really, his information did get logged into the database.

What should happen when properly registered, The message should be "Registered! Thanks you xyz, your information....."....

I could try it if you still think it makes a difference....
Floyd

Re: Join! button does not jump to correct location/page

Posted: Tue Apr 08, 2008 2:34 pm
by MesaFloyd
for what it is worth, here is the database file.. maybe you can see something...

Code: Select all

 
<?
//FK This comes from -- http://www.evolt.org/article/PHP_Login_ ... index.html
 
/**
 * Connect to the mysql database.
 */
 
//FK added this--BEGIN
$dbHost = 'xxx.yyy.zzz.com';    // Database Connection Details - dbHost
$dbUser = 'xxxyyyzzz';               // Database Connection Details - dbUsername
$dbPass = 'xyz';            // Database Connection Details - dbPassword
$dbname = 'xyzxyz';     // Database Connection Details - Database Name
 
$conn = mysql_connect($dbHost,$dbUser,$dbPass) or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
 
?>
 
 

Re: Join! button does not jump to correct location/page

Posted: Wed Apr 09, 2008 8:33 am
by bovermyer
I tried it out on my dev server, and it works fine. What version of PHP are you using?