Jump to content

Fatal error: Cannot redeclare escape_data() (previously declared)


BrentonHale

Recommended Posts

When I submit my password.php page to change a user password -- I'm getting this error:

 

Fatal error:  Cannot redeclare escape_data() (previously declared in C:\wamp\www\mysql_connect.php:20) in C:\wamp\www\password.php on line 14

 

I tried deleting at specific line out of the code in both pages, then my connection gets reset.  I'm including both page's code.  password.php and mysql_connect.php

 

 

 

password.php

 

 

[<?php # Script 7.8 - password.php
// This page lets a user change their password.

// Set the page title and include the HTML header.
$page_title = 'Change Your Password';
include ('./header.html');

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

    require_once ('mysql_connect.php'); // Connect to the db.
    
    // Create function for escaping the data.
    function escape_data ($data) {
        global $dbc; // Need the connection.
        if (ini_get('magic_quotes_gpc')) {
            $data = stripslashes($data);
        }
        return mysql_real_escape_string (trim($data), $dbc);
    } // End of function.
    
    $errors = array(); // Initialize error array.
    
    // Check for an email address.
    if (empty($_POST['email'])) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = escape_data($_POST['email']);
    }
    
    // Check for an existing password.
    if (empty($_POST['password'])) {
        $errors[] = 'You forgot to enter your existing password.';
    } else { 
        $p = escape_data($POST['password']);
    }
    
    // Check for a password and match against the confirmed password.
    if (!empty($_POST['password1'])) {
            if ($_POST['password1'] != $_POST['password2']) {
                $errors[] = 'Your new password did not match the confirmed new password.';
    } else {
        $np = escape_data($_POST['password1']);
        }
    } else { 
        $errors[] = 'You forgot to enter your new password.';
    }
    
    if (empty($errors)) { // If everythings OK.
    
        // Check that theyve entereed the right email address/password combination.
        $query = "SELECT user_id FROM users WHERE (email='$e' AND password=SHA('$p') )";
        $result = mysql_query($query);
        $num = mysql_num_rows($result);
        if (mysql_num_rows($result) == 1) { // Match was made.
        
            // Get the user_id.
            $row = mysql_fetch_array ($result, MYSQL_NUM);
            
            // Make the UPDATE query.
            $query = "UPDATE users SET password=SHA('$np') WHERE user_id=$row[0]";
            $result = @mysql_query ($query);
            if (mysql_affected_rows() == 1) { // If it ran OK.
            
                // Send an email, if desired.
                
                // Print a message.
                echo'<h1 id="mainhead">Thank you!</h1>
                <p>Your password has been updated.  In Chapter 9 you will actually be able to log in! </p><p><br/></p>';
                
                // Include the footer and quit the script (to not show the form).
                include ('./footer.html');
                exit();
                
            } else { // If it did not run OK.
                echo '<h1 id="mainhead">System Error</h1>
                <p class="error">Your password could not be changed due to a system error. We apologize.</p>'; // Public message.
                echo '<p>' . mysql_error() . '<br/><br/>Query: ' . $query . '</p>'; // Debugging message.
                include ('./footer.html');
                exit();
            }
            
        } else { // Invalid email address/password combination.
            echo '<h1 id="mainhead">Error!</h1>
            <p class="error">The email address and password do not match those on file.</p>';
        } 
        
    } else { // Report the errors.
    
        echo '<h1 id="mainhead">Error!</h1>
        <p class="error">The following errors(s) occurred:<br/>';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br/>\n";
        }
        echo '</p><p>Please try again.</p><p><br/></p>';
        
    } // End of if (empty($errors)) IF.
    
        mysql_close(); // Close the database connection.
        
    } // End of the main Submit conditional.
    ?>
    <h2>Change Your Password</h2>
    <form action="password.php" method="post">
        <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
        <p>Current Password: <input type="password" name="password" size="10" maxlength="20" /></p>
        <p>New Password: <input type="password" name="password1" size="10" maxlength="20" /></p>
        <p>Confirm New Password: <input type="password" name="password2" size="10" maxlength="20" /></p>
        <p><input type="submit" name="submit" value="Change My Password" /></p>
        <input type="hidden" name="submitted" value="TRUE" />
    </form>
    <?php
    include ('./footer.html');
    ?>

 

 

and

 

 

mysql_connect.php

 

 

<?php # Script 8.1 - mysql_connect.php

// This file contains the database access information.
// This file also establishes a connection to MySQL and selects the database.
// This file also defines the escape_data() function.

// Set the database access information as constants.
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');

// Make the connection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());

// Select the database.
@mysql_select_db (DB_NAME) OR DIE ('Could not select the database: ' . mysql_error());

// Create a function for escaping the data.
function escape_data ($data) {

// Address Magic Quotes.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}

// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string')) {
global $dbc; // Need the connection.
$data = mysql_real_escape_string (trim($data), $dbc);
} else {
$data = mysql_escape_string (trim($data));
}

// return the escaped value.
return $data;

} // End of function.
?>

 

 

Link to comment
Share on other sites

remove the function from password.php.

 

When I removed all of the code for that function (lines 13 - 20):  I get connection was reset in the web browser.

 

When I remove only

function escape_data ($data) {

,  I get Parse error:  parse error in C:\wamp\www\password.php on line 99

 

I've checked the code four times now for typo's and can't find any.

Link to comment
Share on other sites

Try changing your function in the mysql page to this:

 

// Create a function for escaping the data.
function escape_data ($data) {
// Address Magic Quotes.
if (get_magic_quotes_gpc()) {
	$data = stripslashes($data);
}
$data = trim($data);

// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string')) {
	$data = mysql_real_escape_string ($data);
}else {
	$data = mysql_escape_string ($data);
}

// return the escaped value.
return $data;
} // End of function.

 

Just re-vamped it a bit and removed the global $dbc, as that is only needed if you are using MySQLi, as the connection is global in itself.

Link to comment
Share on other sites

So you removed all of:

 

    // Create function for escaping the data.
    function escape_data ($data) {
        global $dbc; // Need the connection.
        if (ini_get('magic_quotes_gpc')) {
            $data = stripslashes($data);
        }
        return mysql_real_escape_string (trim($data), $dbc);
    } // End of function.

 

? If not remove that whole section. If so, please point out what line 99 is.

Link to comment
Share on other sites

So you removed all of:

 

    // Create function for escaping the data.
    function escape_data ($data) {
        global $dbc; // Need the connection.
        if (ini_get('magic_quotes_gpc')) {
            $data = stripslashes($data);
        }
        return mysql_real_escape_string (trim($data), $dbc);
    } // End of function.

 

? If not remove that whole section. If so, please point out what line 99 is.

 

When I remove all of that code above and go to click on "change my password"  I receive this message in the browser:

 

The connection was reset

         

 

The connection to the server was reset while the page was loading

 

    *  The site could be temporarily unavailable or too busy. Try again in a few

          moments.

 

    *  If you are unable to load any pages, check your computer's network

          connection.

 

    *  If your computer or network is protected by a firewall or proxy, make sure

          that Firefox is permitted to access the Web.

 

And when I leave the above code, I get the Parse error: .  Either way it's not working.

Link to comment
Share on other sites

Usually when you get the connection is reset it means there is or was an infinite loop. Check the other included files and make sure that there are no infinite loops. Also, I would restart your apache server.

 

If this is on a Shared remote host, it could be someone else's script and it may just be your host.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.