-6

This is my code.

<? 
  define('DB_HOST', 'localhost'); 
  define('DB_NAME', 'practice'); 
  define('DB_USER','root'); 
  define('DB_PASSWORD',''); 
  $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
  $db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); 
  /* $ID = $_POST['user']; $Password = $_POST['pass']; */ 
  function SignIn() { 
      session_start(); //starting the session for user profile page 
      if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text 
      { 
          $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 
          $row = mysql_fetch_array($query) or die(mysql_error()); 
          if(!empty($row['userName']) AND !empty($row['pass'])) { 
             $_SESSION['userName'] = $row['pass']; echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 
          } else { 
             echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
          } 
    } 
 } 
 if(isset($_POST['submit'])) { SignIn(); } 
 ?>

I try to run it but it had a blank screen. PS. Sorry for my English, I am Thai.

Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • 1
    I downvoted because if we cannot read your code, we cannot help you. http://idownvotedbecau.se/unreadablecode/ – Milan Chheda Jan 21 '18 at 04:32
  • 2
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Jan 21 '18 at 04:48
  • 1
    When you get a blank screen (but you aren't expecting it), check your error logs before posting a question. – mickmackusa Jan 21 '18 at 04:48

1 Answers1

0

You have missed closing brace } of function SignIn() for which it generates syntax error.

Your Code (with error):

<?
  define('DB_HOST', 'localhost'); 
  define('DB_NAME', 'practice'); 
  define('DB_USER','root'); 
  define('DB_PASSWORD',''); 
  $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
  $db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
  /* $ID = $_POST['user']; $Password = $_POST['pass']; */
  function SignIn() {
    session_start(); //starting the session for user profile page if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text { $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); $row = mysql_fetch_array($query) or die(mysql_error()); if(!empty($row['userName']) AND !empty($row['pass'])) { $_SESSION['userName'] = $row['pass']; echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; } else { echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; } } } if(isset($_POST['submit'])) { SignIn(); } 
?>

Below code will work (with fix)

<?
  define('DB_HOST', 'localhost'); 
  define('DB_NAME', 'practice'); 
  define('DB_USER','root'); 
  define('DB_PASSWORD',''); 
  $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
  $db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
  /* $ID = $_POST['user']; $Password = $_POST['pass']; */
  function SignIn() {
    session_start(); //starting the session for user profile page if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text { $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); $row = mysql_fetch_array($query) or die(mysql_error()); if(!empty($row['userName']) AND !empty($row['pass'])) { $_SESSION['userName'] = $row['pass']; echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; } else { echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; } } } if(isset($_POST['submit'])) { SignIn(); }
  }
?>

Why don't you enable errors for your PHP code to see the errors? Below is the code if you put at the beginning of you file will start displaying errors.

<?php
  ini_set('display_errors', 'On');
  error_reporting(E_ALL);
?>

Also, session_start(); should always be at the beginning of the page, not inside any function.

Somnath Sinha
  • 662
  • 6
  • 16
  • 1
    Regarding your statement about `session_start()`, I disagree, it could well be as far down the execution path as you have not sent output yet. – Majid Fouladpour Jan 21 '18 at 04:36
  • the curly brackets are there, with a matching set of opening and closing – Ahmad Jan 21 '18 at 04:36
  • It doesn't. Why? I already install PHP environment. It doesn't show anything. – ChanakanZ Programmer Jan 21 '18 at 04:37
  • @Majid Fouladpour: I have never said that it can not be used, I have said session_start(); should be called at the beginning to avoid errors. From my experience, I have seen a lot of websites with a **Notice** because of the some error thrown before the use of `session_start()` – Somnath Sinha Jan 21 '18 at 04:42
  • 1
    @ChanakanZ Programmer: You are using PHP short tag, and to use that you must have short_open_tag enabled in your **php.ini**. For more details on this configuration, see http://php.net/manual/en/language.basic-syntax.phptags.php – Somnath Sinha Jan 21 '18 at 04:46