Login Form & Signup Form Not Working In Infinityfree.net Database

Website: http://valorantlfg.ga/
when u tried to go in signup page or signin page it will land you into this

Sources:
Signup PHP:

    <?php 

    session_start();

        include("connection.php");

        include("functions.php");

        if($_SERVER['REQUEST_METHOD'] == "POST")

        {

            //something was posted

            $user_name = $_POST['user_name'];

            $password = $_POST['password'];

            if(!empty($user_name) && !empty($password) && !is_numeric($user_name))

            {

                //save to database

                $user_id = random_num(20);

                $query = "insert into users (user_id,user_name,password) values ('$user_id','$user_name','$password')";

                mysqli_query($con, $query);

                header("Location: login.php");

                die;

            }else

            {

                echo "Please enter some valid information!";

            }

        }

    ?>

Login Php:

    <?php 

    session_start();

      include("connection.php");

      include("functions.php");

      if($_SERVER['REQUEST_METHOD'] == "POST")

      {

        //something was posted

        $user_name = $_POST['user_name'];

        $password = $_POST['password'];

        if(!empty($user_name) && !empty($password) && !is_numeric($user_name))

        {

          //read from database

          $query = "select * from users where user_name = '$user_name' limit 1";

          $result = mysqli_query($con, $query);

          if($result)

          {

            if($result && mysqli_num_rows($result) > 0)

            {

              $user_data = mysqli_fetch_assoc($result);

              

              if($user_data['password'] === $password)

              {

                $_SESSION['user_id'] = $user_data['user_id'];

                header("Location: pages/Home");

                die;

              }

            }

          }

          

          echo "<h3>wrong username or password!</h3>";

        }else

        {

          echo "<h3>wrong username or password!</h3>";

        }

      }

    ?>

functions php:

    <?php

    function check_login($con)

    {

        if(isset($_SESSION['user_id']))

        {

            $id = $_SESSION['user_id'];

            $query = "select * from users where user_id = '$id' limit 1";

            $result = mysqli_query($con,$query);

            if($result && mysqli_num_rows($result) > 0)

            {

                $user_data = mysqli_fetch_assoc($result);

                return $user_data;

            }

        }

        //redirect to login

        header("Location: login.php");

        die;

    }

    function random_num($length)

    {

        $text = "";

        if($length < 5)

        {

            $length = 5;

        }

        $len = rand(4,$length);

        for ($i=0; $i < $len; $i++) { 

            # code...

            $text .= rand(0,9);

        }

        return $text;

    }

connection php:

    <?php

    $dbhost = "localhost";

    $dbuser = "root";

    $dbpass = "";

    $dbname = "login_sample_db";

    if(!$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname))

    {

        die("failed to connect!");

    }

Logout php:

<?php

    session_start();

    if(isset($_SESSION['user_id']))

    {

        unset($_SESSION['user_id']);

    }

    header("Location: /");

    die;
    For Logined page:
    <?php 

    session_start();

        include("connection.php");

        include("functions.php");

        $user_data = check_login($con);
```

connections php:

```
<?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "login_sample_db"; if(!$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname)) { die("failed to connect!"); } Logout: <?php session_start(); if(isset($_SESSION['user_id'])) { unset($_SESSION['user_id']); } header("Location: /"); die; For Logined page: <?php session_start(); include("connection.php"); include("functions.php"); $user_data = check_login($con); ?>
```

Logout php:

```
<?php session_start(); if(isset($_SESSION['user_id'])) { unset($_SESSION['user_id']); } header("Location: /"); die; For Any Logined page: <?php session_start(); include("connection.php"); include("functions.php"); $user_data = check_login($con); ?>
```

For Any Logined Page:

```
<?php session_start(); include("connection.php"); include("functions.php"); $user_data = check_login($con); ?>
```

I used XAMPP Because its working but if i used Infinityfree database it will stop working now

Please format your code. Surround it by

”~~~”

So it won’t get auto-formatted.

Thanks.

thanks for saying

I updated the code to make it a bit more readable. The forum supports HTML, BBCode or Markdown for formatting. Please use some sort of code block when sharing code because the parser mangles it if you don’t. And avoid formatting which means other things in other formatting languages, like prefixing text lines with some blank space (which is one way to do code formatting with Markdown).

Also it’s not very nice to just dump your entire codebase on someone and say “it’s not working, please fix”. It’s your code base, you know how it should work and you’re in the best position to debug it.

As for the connection.php file:

  • Are the credentials in there also what you’re trying to use on our hosting? Because you need to use the database credentials we provide, not what you’ve used in XAMPP.
  • Please implement error checking that also checks the error message. So if mysqli_connect returns false, then use mysqli_connect_error to retrieve the error message why it fails.
1 Like

i used mysqli_connect_error but it says

Fatal error: Uncaught Error: Class "mysqli_connect_error" not found in C:\xampp\htdocs\connection.php:8 Stack trace: #0 C:\xampp\htdocs\Login.php(5): include() #1 {main} thrown in C:\xampp\htdocs\connection.php on line 8

How are you using mysqli_connect_error? The error implies PHP thinks you’re trying to use it as a class, not a function, which makes me believe that you’re not using it correctly.

Also, how does this relate to your other topic? It doesn’t help anyone if you’re discussing the same issue in multiple threads.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.