-1

I have made a website and i needed a login and sign up page. When i try it it dosen't work. It keeps popping up with unable to register. Please could you help. Here is the register page

 <?php

 session_start();
if(isset($_SESSION['users22'])!="")
{
header("Location: Home1.php");
}
include_once 'dbConnect.php';

if(isset($_POST['btn-signup']))
{
 $uname = mysqli_real_escape_string($_POST['uname']);
 $email = mysqli_real_escape_string($_POST['email']);
 $upass = md5(mysqli_real_escape_string($_POST['pass']));

if(mysqli_query("INSERT INTO users22(username,email,password)  
VALUES('$uname','$email','$upass')"))
 {
?>
   <script>alert('successfully registered ');</script>
      <?php
}
else
{
?>
     <script>alert('Error while registering you. Please try again.');</script>
    <?php
}
}
 ?>

Here is the login page:

<?php
session_start();
include_once 'dbConnect.php';

if(isset($_SESSION['users22'])!="")
{
header("Location: Home1.php");
}
 if(isset($_POST['btn-login']))
 {

$email = mysqli_real_escape_string($_POST['email']);
$upass = mysqli_real_escape_string($_POST['pass']);
$res=mysqli_query("SELECT * FROM users22 WHERE email='$email'");
$row=mysqli_fetch_array($res);
if($row['password']==md5($upass))
{
$_SESSION['users22'] = $row['user_id'];
 header("Location: Home1.php");
}
else
{
 ?>
      <script>alert('wrong details');</script>
      <?php
 }

  }
 ?>

Thank You very much.

  • `if(isset($_SESSION['users22'])!="")` I've seen this so many places, I don't get where it's coming from - but *this does not do what you think it does!* - basically what you get is `if (true != '')`.. – Qirel Sep 16 '16 at 19:01
  • Your code is failing you for quite a few reasons and not just the *false positive* on `if(isset($_SESSION['users22'])!="")`. Plus, who knows which MySQL API you're using to connect with. – Funk Forty Niner Sep 16 '16 at 19:13

1 Answers1

1

isset($_SESSION['users22']) this is creaing issue you cant use like this same for login page. Also i am assuming $con as DB connection

 session_start();
if(isset($_SESSION['users22']) && $_SESSION['users22'] !="")
{
header("Location: Home1.php");
}
include_once 'dbConnect.php';

if(isset($_POST['btn-signup']))
{
 $uname = mysqli_real_escape_string($con, $_POST['uname']);
 $email = mysqli_real_escape_string($con, $_POST['email']);
 $upass = md5(($_POST['pass']);

 $sql = "INSERT INTO users22(username,email,password) VALUES('$uname','$email','$upass')";
if(mysqli_query($con, $sql))
 {
?>
   <script>alert('successfully registered ');</script>
      <?php
}
else
{
?>
     <script>alert('Error while registering you. Please try again.');</script>
    <?php
}
}
 ?>
Farhan
  • 1,453
  • 2
  • 15
  • 20
  • You missed quite a few things in their mysql functions. What you posted, will fail them since you basically pasted their same errors but only fixing the one thing. – Funk Forty Niner Sep 16 '16 at 19:12
  • Sorry but not much familiar with core PHP should i indicate if condition in answer or its fine @Fred-ii- – Farhan Sep 16 '16 at 19:14
  • Then this `if(isset($_SESSION['users22']) && isset($_SESSION['users22']) !="")` - that's incorrect. Again; same mistake. – Funk Forty Niner Sep 16 '16 at 19:14
  • Look at the following manuals on `mysqli_query()` and `mysqli_real_escape_string()`. http://php.net/manual/en/mysqli.query.php --- http://php.net/manual/en/mysqli.real-escape-string.php - Read those and you will learn a few things, as will the person who posted the question. They're probably from a `mysql_` background and think they can use the same syntax. – Funk Forty Niner Sep 16 '16 at 19:17
  • Sorry actually i was pointing this not sure how i missed this in answer – Farhan Sep 16 '16 at 19:17
  • I hope i have done like on manual :) @Fred-ii- – Farhan Sep 16 '16 at 19:25
  • Yes, I noticed the edit ;-) – Funk Forty Niner Sep 16 '16 at 19:26
  • 1
    This goes for me 1000 plus thanks @Fred-ii- – Farhan Sep 16 '16 at 19:27