Issues with PHP login functionality

I have been struggling with this issue since last night, so I finally decided to reach out to ask for your help.

Can anyone guide me on how to solve this problem? Are there any mistakes in the code provided below? I'm fairly new to PHP and would appreciate any assistance.

<?php

$connection = mysql_connect($HOST,$USER,$PASS) Or Die ("Could not connect to the server");
mysql_select_db($DBNAME, $connection )Or Die ("Could not connect to the server");

error_reporting(0);

if ($_POST['Register']);{
  if($_POST['Username'] && $_POST['Password']);
  $username = mysql_real_escape_string($_POST['Username']);
  $password = mysql_real_escape_string(hash("sha512",$_POST['Password']));
  $name ='';
  if($_POST['Name']){
    $name = mysql_real_escape_string(strip_tags($_POST[name]));
  }
  $check = mysql_fetch_array(mysql_query("SELECT * FROM 'users' WHERE 'Username'='$username'"));
  if ($check != '0'){
    die("Uh Oh! That Username has already taken! Try <i>$username" . rand (1,50) . "</i> instead! <a href='register.php'>&larr; Back </a>" );
  }
  if (!ctype_alnum($username)){
    die("Uh hum ! Your username conatins special characters unfortunately they are not permitted ! Only letters and numbers are allowed! <a href='register.php'>&larr; Back </a>");
  }
  if (strlen($username) >20){
    die("Username Cannot contain more than 20 characters <a href='register.php'>&larr; Back </a>");
  }
  $salt = hash("sha512", rand() .rand(). rand());
  mysql_query("INSERT INTO 'users' ('Username','Password','Name','Salt') VALUES('$username','$password','$name','$salt')");
  set_cookie("c_user", hash("sha512",$username),time() + 24 * 60 * 60, "/" );
  set_cookie("c_salt", $salt, $time () + 24 * 60 * 60, "/");
  die ("Congrts ! you are now ready to use Hack With Nick! You are now logged in !");
}

?>

<body style='font-family: sans-serif,verdana;'>
 <div style='width: 80%; padding: 5px 15px 5px ; border: 1px solid #e3e3e3; background-color: #fff; color:000 ; margin-left:auto; margin-right:auto;'>
  <h1>Register</h1>
  <br />
   <form action='' method='post' >
    <table>
     <tr>
      <td>
      <b>Username:</b>
      </td>
      <td>
       <input type='text' name='username' style='padding: 4px;' />
      </td>
     </tr>
     <tr>
      <td>
       <b>Password:</b>
      </td>
      <td>
      <input type='password' name='password' style='padding:4px;' />
      </td>
     </tr>
     <tr>
      <td>
       <b>Name:</b>
      </td>
      <td>
       <input type='text' name='name' style='padding:4px;' />
      </td>
     </tr>
     <tr>
      <td>
       <input type='submit' name='register' value='Register' />
      <td>
     </tr>
    </table>
  </form>
 </div>
</body>

Answer №1

It is recommended to start by enabling error reporting for better troubleshooting.

error_reporting(E_ALL);
ini_set('display_errors','1');

Also, ensure that your input names are in lowercase and fix any missing quotes in the name field. Without braces in your if statements, they may not function as intended. Additionally, remember what Iserni pointed out: There are errors in your SQL code - it should be 'users' instead of users, and use time() instead of $time if you want to utilize PHP's built-in function.

if (isset($_POST['register'])){
  if(!empty($_POST['username']) && !empty($_POST['password'])){
      $username = mysql_real_escape_string($_POST['username']);
      $password = mysql_real_escape_string(hash("sha512",$_POST['password']));
      $name ='';
      if(isset($_POST['name'])){
        $name = mysql_real_escape_string(strip_tags($_POST['name']));
      }
      $check = mysql_query("SELECT * FROM users WHERE 'Username'='$username'");
      if (mysql_num_rows($check) != 0){
        die("Uh Oh! That Username has already taken! Try <i>$username" . rand (1,50) . "</i> instead! <a href='register.php'>← Back </a>" );
      }
      if (!ctype_alnum($username)){
        die("Uh hum ! Your username conatins special characters unfortunately they are not permitted ! Only letters and numbers are allowed! <a href='register.php'>← Back </a>");
      }
      if (strlen($username) >20){
        die("Username Cannot contain more than 20 characters <a href='register.php'>← Back </a>");
      }
      $salt = hash("sha512", rand() .rand(). rand());
      mysql_query("INSERT INTO users ('Username','Password','Name','Salt') VALUES('$username','$password','$name','$salt')");
      set_cookie("c_user", hash("sha512",$username),time() + 24 * 60 * 60, "/" );
      set_cookie("c_salt", $salt, time() + 24 * 60 * 60, "/");
      die ("Congrts ! you are now ready to use Hack With Nick! You are now logged in !");
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP Array Element Output

After making a call to the Twitter API, I have received an Array of data containing the "Top 10 Trending" items. However, I am facing difficulty in printing out the individual elements such as names. Below is the code snippet that I have been using to disp ...

Learn how to use AJAX to send a post request to PHP and dynamically load content into a specific

Recently, I have been working on developing a filter function using ajax and PHP. Although I am still quite new to ajax, I was able to create an HTML input that looks like this: <form> <input type="text" placeholder="Search" ...

Uploading an image to an Oracle database with the help of PHP and AJAX

Looking for a way to insert an image into an Oracle database using OCI with PHP, Ajax, and jQuery. I've searched online but couldn't find any examples. If anyone has a solution, please share. Thanks in advance. <la ...

What is the best way to simulate an annotated method from AWS in PHPUnit?

Currently, I am in the process of writing a unit test to verify if the method publish() has been called at least once. Here is a snippet from my test class: <?php namespace App\Tests\Unit; use Aws\Sns\SnsClient; use Exception; use ...

php code for locating a substring within a larger string

I need assistance with accessing a mysql database and extracting a specific segment of code from a column. The content in the column looks something like this: <image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5"> <file><![CDATA[image ...

Utilizing PHP for XML exportation and fetching it through AJAX to integrate it into the DOM, unfortunately, the XML content remains invisible

I've encountered a strange issue with a PHP script that generates valid XML output. I'm trying to fetch this data using an Ajax XMLHttpRequest call in the browser. Although Firebug confirms that the Ajax request is successful and the XML is vali ...

Is there a way to customize the color of specific sections on a dygraph chart?

I am looking to incorporate dygraphs into my website, but I need help with displaying a specific background color in certain parts of the chart. For example, I want the chart to show green during daylight hours. Can you assist me in achieving this? ...

Displaying Form Results on the Same Page in Drupal: A Step-by-Step Guide

Is there a way to display the results of a form submission on the same page as the form itself? Here is the relevant hook_menu code: $items['admin/content/ncbi_subsites/paths'] = array( 'title' => 'Paths', ...

Is it possible for .php files that don't contain any PHP code to be sent to the interpreter?

Does the PHP interpreter process .php files on a standard LAMP stack even if they do not contain any PHP code? In essence, is there any performance or processing impact in having a .php file without any actual PHP code compared to simply making it an .htm ...

Organize in descending order based on the number of references in

https://i.stack.imgur.com/CnRbZ.png Is there a way to sort by reference count? In the image displayed above, 2-1 1-1 I wish to achieve this output. Please excuse any mistakes in my English ...

Ajax is coming back with a value that is not defined

Currently, I am working on a search function that is responsible for searching a name from the database. When the user clicks "add", the selected item should appear below the search field so that it can be saved in the database. However, I am encountering ...

Exploring the dynamic duo of MongoDB and GridFS

We currently manage a large-scale project that accommodates thousands of users daily. Our database system is MySQL, but we are considering transitioning to MongoDB along with GridFS. Is it feasible to utilize MongoDB and GridFS for projects on this scale? ...

What is the process for converting a SEF URL to a non-SEF URL for a single view in Joomla?

I am currently working on developing an API type plugin that retrieves activity streams from easysocial. However, I have encountered SEF URLs in the activity stream, but I would like to display NON-SEF URLs when SEF is enabled in Joomla's configuratio ...

"PHP MySQL news error" - An unexpected error occurred

Just incorporated a news system using MySQL/PHP, it functions flawlessly; however, encounters errors when inserting HTML. For instance, inserting a YouTube video results in the addition of / or \ Can someone advise on what changes need to be made in ...

require_once function causing no errors but resulting in a blank, non-responsive screen

Having trouble loading models with the code snippet below. There seems to be an issue with require_once causing the page to crash. The fact that "not there" is not echoed indicates that the file does exist. protected function modelFactory ($model, $input ...

Access values from the view in Blade without passing them as parameters

Is there a way to retrieve a variable named "foo" from a Blade template and pass it to a static function defined within the same template? For instance, let's say we have: <div class="collumns large-8"> {{ Helpers::setLetters('variable_n ...

Ensuring the Accuracy of Translated Objects within CakePHP 3

I'm currently facing some challenges while trying to validate an I18N field in CakePHP3. The setup for the translate behavior looks like this: $this->addBehavior('Translate', [ 'fields' => ['name', 'body ...

When the headers exceed the POST_MAX_SIZE limit, the $_POST data will be empty

Hey there, I'm looking for some help with a form validation issue. The form I have includes basic fields like name, number, email address, etc., as well as a file upload field. I'm trying to implement validation in my script to check if the upl ...

Ways to eliminate header and footer data while printing a webpage

In my attempt to implement the code displayed below, I am encountering an issue where the header and footer continue to appear on the printed page. Despite numerous attempts with various CSS elements, I have been unsuccessful in removing the header and fo ...