Newbie needs help

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
melindash
Forum Newbie
Posts: 2
Joined: Mon Mar 16, 2009 6:46 pm

Newbie needs help

Post by melindash »

I pulled this code from a tutorial, so that I can get started with it and learn as I go (I really need to get my site off the ground asap). I have a functions.php file (uploaded to my server) that looks like this:

Code: Select all

<?php
// Salt Generator
function generate_salt ()
{ 
     // Declare $salt
     $salt = '';
 
     // And create it with random chars
     for ($i = 0; $i < 3; $i++)
     { 
          $salt .= chr(rand(35, 126)); 
     } 
          return $salt;
}
function user_register($username, $password)
{
     // Get a salt using our function
     $salt = generate_salt();
 
     // Now encrypt the password using that salt
     $encrypted = md5(md5($password).$salt);
     // And lastly, store the information in the database
     $query = "insert into user (username, password, salt) values ('$username', '$encrypted', '$salt')";
     mysql_query ($query) or die ('Could not create user.');
}
function user_login($username, $password)
{
     // Try and get the salt from the database using the username
     $query = "select salt from user where username='$username' limit 1";
     $result = mysql_query($query);
     $user = mysql_fetch_array($result);
     // Using the salt, encrypt the given password to see if it 
     // matches the one in the database
     $encrypted_pass = md5(md5($password).$user['salt']);
 
     // Try and get the user using the username & encrypted pass
     $query = "select userid, username from user where username='$username' and password='$encrypted_pass'";
     $result = mysql_query($query);
     $user = mysql_fetch_array($result);
     $numrows = mysql_num_rows($result);
     // Now encrypt the data to be stored in the session
     $encrypted_id = md5($user['userid']);
     $encrypted_name = md5($user['username']);
     // Store the data in the session
     $_SESSION['userid'] = $userid;
     $_SESSION['username'] = $username;
     $_SESSION['encrypted_id'] = $encrypted_id;
     $_SESSION['encrypted_name'] = $encrypted_name;
    if ($numrows == 1)
    {
        return 'Correct';
    }
    else
    {
        return false;
    }
}
function user_logout()
{
     // End the session and unset all vars
     session_unset ();
     session_destroy ();
}
function is_authed()
{
     // Check if the encrypted username is the same
     // as the unencrypted one, if it is, it hasn't been changed
     if (isset($_SESSION['username']) && (md5($_SESSION['username']) == $_SESSION['encrypted_name']))
     {
          return true;
     }
     else
     {
          return false;
     }
}
?>
I also have a webpage called register.php that looks something like this:
(excluding the template--this is just the form and actions)

Code: Select all

<?php if (isset($reg_error)) { ?>
There was an error: <?php echo $reg_error; ?>, please try again.
<?php } ?>
<form action="register.php" method="post">
<b>Username:</b> <input type="text" size="20" maxlength="20" name="username" 
<?php if (isset($_POST['username'])) { ?> value="<?php echo $_POST[ 'username' ]; ?>" <?php } ?>/><br />
<b>Password:</b> <input type="password" size="20" maxlength="10" name="password" /><br />
<b>Confirm Password:</b> <input type="password" size="20" maxlength="10" name="confirmpass" /><br />
<input type="submit" name="submit" value="Register!" />
</form>
<?php
// Include init file
include 'init.php';
if (!isset($_POST['submit']))
{
     // Show the form
     include 'register_form.inc.php';
     exit;
}
else
{
     // Check if any of the fields are missing
     if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']))
     {
          // Reshow the form with an error
          $reg_error = 'One or more fields missing';
          include 'register_form.inc.php';
          exit;
     }
 
     // Check if the passwords match
     if ($_POST['password'] != $_POST['confirmpass'])
     {
          // Reshow the form with an error
          $reg_error = 'Your passwords do not match';
          include 'register_form.inc.php';
          exit;
     }
     // Everything is ok, register
     user_register ($_POST['username'], $_POST['password']);
 
     echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';
}
?>
When I go to http://www.mysite.com/register.php, I get this error message:
"Parse error: syntax error, unexpected T_STRING in /home/content/m/e/l/melindash/html/functions.php on line 6"

line 6 of functions.php :

Code: Select all

$salt = '';
I have no idea what is wrong. Please help, I've been stuck on this for days.
Thanks a bunch, Melinda
Last edited by melindash on Mon Mar 16, 2009 6:57 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Newbie needs help

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Post Reply