How to open a popup window if login successful

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Slaxer13
    New Member
    • Jun 2014
    • 106

    How to open a popup window if login successful

    Hi guys, i am currently doing a php app for school and the question is this, how can i make a popup window appear if the login is successful and redirect to another page (for example index.php) automatically in, lets say, 5 secs?

    My login page as the following code (i am portuguese so some things are in PT but just say and i will translate),

    Code:
    <?php require_once('Connections/Info_Registos.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    mysql_select_db($database_Info_Registos, $Info_Registos);
    $query_Login = "SELECT * FROM utilizador";
    $Login = mysql_query($query_Login, $Info_Registos) or die(mysql_error());
    $row_Login = mysql_fetch_assoc($Login);
    $totalRows_Login = mysql_num_rows($Login);
    ?>
    <?php
    // *** Validate request to login to this site.
    if (!isset($_SESSION)) {
      session_start();
    }
    
    $loginFormAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
      $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    }
    
    if (isset($_POST['utilizador'])) {
      $loginUsername=$_POST['utilizador'];
      $password=$_POST['password'];
      $MM_fldUserAuthorization = "";
      $MM_redirectLoginSuccess = "index.php";
      $MM_redirectLoginFailed = "login_incorreto.php";
      $MM_redirecttoReferrer = false;
      mysql_select_db($database_Info_Registos, $Info_Registos);
      
      $LoginRS__query=sprintf("SELECT utilizador, password FROM utilizador WHERE utilizador=%s AND password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
       
      $LoginRS = mysql_query($LoginRS__query, $Info_Registos) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        
    	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
        //declare two session variables and assign them
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
    
        if (isset($_SESSION['PrevUrl']) && false) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
        }
        header("Location: " . $MM_redirectLoginSuccess );
      }
      else {
        header("Location: ". $MM_redirectLoginFailed );
      }
    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Login</title>
    <style type="text/css">
    @import url("style.css");
    </style>
    </head>
    
    <body>
    	<h1><img src="images/Logo PAP 2.png" alt="" width="327" height="242" align="right">Login</h1>
    <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="Login">
      <label>Utilizador:<br/></label>
        <input type="text" name="utilizador"><br/>
        <label>Password:<br/></label>
        <input type="password" name="password"><br/><br/>
        <input type="submit" value="Login"><br/><br/>
        </form>
        <a href="registo.php">Registar</a>
    </body>
    </html>
    <?php
    mysql_free_result($Login);
    ?>
    I'm thankful for any help.

    Peace and love,

    Slaxer13
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Hi Slaxer,

    In the code block
    Code:
    if ($loginFoundUser) {
         $loginStrGroup = "";
    You can add:
    Code:
    ?><script>alert("Login successful!")</script><?php
    Hope that helps!

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      how can i make a popup window appear if the login is successful and redirect to another page (for example index.php) automatically
      hm, what is the point of a popup if you redirect anyways?

      PS. Don’t let DreamWeaver make your PHP code, that’s always a sore sight. better use Prepared Statements if you want to be protected from SQL Injection.

      Comment

      • Slaxer13
        New Member
        • Jun 2014
        • 106

        #4
        Dormilich

        Hi mate,

        The code i have has been retrieved from the web. This is just a school work and i'm still a newbie at this. Any tips will be much appreciated.

        Hi computerfox,

        Could you write how the code should be? i tried but it hasn't been working.

        Thanks to both ;)

        Peace,
        Slaxer13

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          since you’re new, don’t start to learn the outdated and deprecated mysql extension. better use mysqli or PDO (personally I recommend PDO over MySQLi, because it’s more straightforward to use).

          Comment

          • Slaxer13
            New Member
            • Jun 2014
            • 106

            #6
            Any suggestion on any site or article i can look in?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              the linked manual pages should get you started.

              Comment

              • Slaxer13
                New Member
                • Jun 2014
                • 106

                #8
                Thanks for the help i will look into it.
                As for the topic do you know how i can do that popup? I tried computerfox way but either i am doing wrong or something else 'cause its not working :/

                Comment

                • computerfox
                  Contributor
                  • Mar 2010
                  • 276

                  #9
                  Can I see your updated code?

                  Comment

                  • computerfox
                    Contributor
                    • Mar 2010
                    • 276

                    #10
                    Try this:
                    Code:
                    <?php require_once('Connections/Info_Registos.php'); ?>
                    <?php
                    if (!function_exists("GetSQLValueString")) {
                    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
                    {
                      if (PHP_VERSION < 6) {
                        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
                      }
                     
                      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
                     
                      switch ($theType) {
                        case "text":
                          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                          break;    
                        case "long":
                        case "int":
                          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                          break;
                        case "double":
                          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                          break;
                        case "date":
                          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                          break;
                        case "defined":
                          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                          break;
                      }
                      return $theValue;
                    }
                    }
                     
                    mysql_select_db($database_Info_Registos, $Info_Registos);
                    $query_Login = "SELECT * FROM utilizador";
                    $Login = mysql_query($query_Login, $Info_Registos) or die(mysql_error());
                    $row_Login = mysql_fetch_assoc($Login);
                    $totalRows_Login = mysql_num_rows($Login);
                    ?>
                    <?php
                    // *** Validate request to login to this site.
                    if (!isset($_SESSION)) {
                      session_start();
                    }
                     
                    $loginFormAction = $_SERVER['PHP_SELF'];
                    if (isset($_GET['accesscheck'])) {
                      $_SESSION['PrevUrl'] = $_GET['accesscheck'];
                    }
                     
                    if (isset($_POST['utilizador'])) {
                      $loginUsername=$_POST['utilizador'];
                      $password=$_POST['password'];
                      $MM_fldUserAuthorization = "";
                      $MM_redirectLoginSuccess = "index.php";
                      $MM_redirectLoginFailed = "login_incorreto.php";
                      $MM_redirecttoReferrer = false;
                      mysql_select_db($database_Info_Registos, $Info_Registos);
                     
                      $LoginRS__query=sprintf("SELECT utilizador, password FROM utilizador WHERE utilizador=%s AND password=%s",
                        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
                     
                      $LoginRS = mysql_query($LoginRS__query, $Info_Registos) or die(mysql_error());
                      $loginFoundUser = mysql_num_rows($LoginRS);
                      if ($loginFoundUser) {
                         $loginStrGroup = "";
                     
                        if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
                        //declare two session variables and assign them
                        $_SESSION['MM_Username'] = $loginUsername;
                        $_SESSION['MM_UserGroup'] = $loginStrGroup;          
                     
                        if (isset($_SESSION['PrevUrl']) && false) {
                          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];    
                        }
                        ?><script>alert("Login successful!");</script><?php
                        header("Location: " . $MM_redirectLoginSuccess );
                      }
                      else {
                       ?><script>alert("Login failed!");</script><?php
                        header("Location: ". $MM_redirectLoginFailed );
                      }
                    }
                    ?>
                    <!doctype html>
                    <html>
                    <head>
                    <meta charset="utf-8">
                    <title>Login</title>
                    <style type="text/css">
                    @import url("style.css");
                    </style>
                    </head>
                     
                    <body>
                        <h1><img src="http://bytes.com/images/Logo PAP 2.png" alt="" width="327" height="242" align="right">Login</h1>
                    <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="Login">
                      <label>Utilizador:<br/></label>
                        <input type="text" name="utilizador"><br/>
                        <label>Password:<br/></label>
                        <input type="password" name="password"><br/><br/>
                        <input type="submit" value="Login"><br/><br/>
                        </form>
                        <a href="registo.php">Registar</a>
                    </body>
                    </html>
                    <?php
                    mysql_free_result($Login);
                    ?>
                    Also, as Dorm mentioned, why do you want to add a popup for the confirmation and then redirect to another confirmation? Seems un-needed. And yeah, don't use Dreamweaver, Notepad or Notepad++ work better. Or if you're on a Unix/Linux based machine, use vim.
                    Last edited by computerfox; May 21 '15, 06:22 PM. Reason: Username incorrectly spelled.

                    Comment

                    • Slaxer13
                      New Member
                      • Jun 2014
                      • 106

                      #11
                      I will try as soon as possible and post the result. I am using dreamweaver 'cause my teacher choose it :/ I want that popup only and then redirect to my main page. The confirmation window i have will be deleted. It was created because i couldn't do the popup.

                      Peace,
                      Slaxer13

                      Comment

                      • Slaxer13
                        New Member
                        • Jun 2014
                        • 106

                        #12
                        computerfox

                        I tried to do that but it does exactly the same thing

                        Comment

                        • computerfox
                          Contributor
                          • Mar 2010
                          • 276

                          #13
                          Do you have JavaScript enabled?

                          Comment

                          • Slaxer13
                            New Member
                            • Jun 2014
                            • 106

                            #14
                            Sorry for the delay. How can i see that?

                            Comment

                            • computerfox
                              Contributor
                              • Mar 2010
                              • 276

                              #15
                              :facepalm:
                              Visit this page and let me know what you see:



                              Code:
                              <?php require_once('Connections/Info_Registos.php'); ?>
                              <?php
                              if (!function_exists("GetSQLValueString")) {
                              function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
                              {
                                if (PHP_VERSION < 6) {
                                  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
                                }
                               
                                $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
                               
                                switch ($theType) {
                                  case "text":
                                    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                                    break;    
                                  case "long":
                                   break;
                                  case "int":
                                    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                                    break;
                                  case "double":
                                    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                                    break;
                                  case "date":
                                    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                                    break;
                                  case "defined":
                                    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                                    break;
                                }
                                return $theValue;
                              }
                              }
                               
                              mysql_select_db($database_Info_Registos, $Info_Registos);
                              $query_Login = "SELECT * FROM utilizador";
                              $Login = mysql_query($query_Login, $Info_Registos) or die(mysql_error());
                              $row_Login = mysql_fetch_assoc($Login);
                              $totalRows_Login = mysql_num_rows($Login);
                              ?>
                              <?php
                              // *** Validate request to login to this site.
                              if (!isset($_SESSION)) {
                                session_start();
                              }
                               
                              $loginFormAction = $_SERVER['PHP_SELF'];
                              if (isset($_GET['accesscheck'])) {
                                $_SESSION['PrevUrl'] = $_GET['accesscheck'];
                              }
                               
                              if (isset($_POST['utilizador'])) {
                                $loginUsername=$_POST['utilizador'];
                                $password=$_POST['password'];
                                $MM_fldUserAuthorization = "";
                                $MM_redirectLoginSuccess = "index.php";
                                $MM_redirectLoginFailed = "login_incorreto.php";
                                $MM_redirecttoReferrer = false;
                                mysql_select_db($database_Info_Registos, $Info_Registos);
                               
                                $LoginRS__query=sprintf("SELECT utilizador, password FROM utilizador WHERE utilizador=%s AND password=%s",
                                  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
                               
                                $LoginRS = mysql_query($LoginRS__query, $Info_Registos) or die(mysql_error());
                                $loginFoundUser = mysql_num_rows($LoginRS);
                                if ($loginFoundUser) {
                                   $loginStrGroup = "";
                               
                                  if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
                                  //declare two session variables and assign them
                                  $_SESSION['MM_Username'] = $loginUsername;
                                  $_SESSION['MM_UserGroup'] = $loginStrGroup;          
                               
                                  if (isset($_SESSION['PrevUrl']) && false) {
                                    $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];    
                                  }
                                  ?><script>alert("Login successful!");</script><?php
                                  header("Location: " . $MM_redirectLoginSuccess );
                                }
                                else {
                                 ?><script>alert("Login failed!");</script><?php
                                  header("Location: ". $MM_redirectLoginFailed );
                                }
                              }
                              ?>
                              <!doctype html>
                              <html>
                              <head>
                              <meta charset="utf-8">
                              <title>Login</title>
                              <style type="text/css">
                              @import url("style.css");
                              </style>
                              </head>
                               
                              <body>
                                  <h1><img src="http://bytes.com/images/Logo PAP 2.png" alt="" width="327" height="242" align="right">Login</h1>
                              <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" name="Login">
                                <label>Utilizador:<br/></label>
                                  <input type="text" name="utilizador"><br/>
                                  <label>Password:<br/></label>
                                  <input type="password" name="password"><br/><br/>
                                  <input type="submit" value="Login"><br/><br/>
                                  </form>
                                  <a href="registo.php">Registar</a>
                              </body>
                              </html>
                              <?php
                              mysql_free_result($Login);
                              ?>
                              Did you code previously redirect prior to our changes?
                              I updated your switch statement as the "long" case didn't have a break.
                              Can you try now?

                              Comment

                              Working...