-1

Good afternoon, I have the following code it is running right, but I would rather open the login.php screen after inserting the user successfully, I would like it to close the screen, but I am not able to adapt the code, I already tried include a script inside the header like this:

header ("<script>
function closes () {
window.opener = window
window.close ("#")}
</script> ");

but without success, if someone can help me I am grateful!

<?php
session_start();
ob_start();
$btnCadUsuario = filter_input(INPUT_POST, 'btnCadUsuario', FILTER_SANITIZE_STRING);
if($btnCadUsuario){
    include_once 'conexao.php';
    $dados_rc = filter_input_array(INPUT_POST, FILTER_DEFAULT);
    
    $erro = false;
    
    $dados_st = array_map('strip_tags', $dados_rc);
    $dados = array_map('trim', $dados_st);
    
    //var_dump($dados);
    if(!$erro){
        //var_dump($dados);
        $result_usuario = "INSERT INTO usuarios (nome, email, usuario, senha) VALUES (
                        '" .$dados['nome']. "',
                        '" .$dados['email']. "',
                        '" .$dados['usuario']. "',
                        '" .$dados['senha']. "'
                        )";
        $resultado_usario = mysqli_query($conn, $result_usuario);
        if(mysqli_insert_id($conn)){
            $_SESSION['msgcad'] = "Usuário cadastrado com sucesso";
            header("Location: login.php");
        }else{
            $_SESSION['msg'] = "Erro ao cadastrar o usuário";
        }
    }
    
}
?>
Fahim Bagar
  • 798
  • 7
  • 17
  • 1
    You sure meant to tag this question `JavaScript` instead of `Java`, I guess? – bkis Aug 04 '20 at 19:35
  • Your code _already_ "open(s) the login.php screen after inserting the user successfully", so your question is unclear. – GrumpyCrouton Aug 04 '20 at 19:36
  • 2
    [Little Bobby](http://bobby-tables.com/) says [you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/). Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). – GrumpyCrouton Aug 04 '20 at 19:37
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Additionally the procedural interface has less rigorous error checking and reporting, frustrating debugging efforts. – tadman Aug 04 '20 at 19:42
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add any data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or data *of any kind* directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Aug 04 '20 at 19:42
  • **WARNING**: Writing an access control layer is not easy and there are many opportunities to get it severely wrong. Any modern [development framework](https://www.cloudways.com/blog/best-php-frameworks/) like [Laravel](http://laravel.com/) comes with an [authentication system](https://laravel.com/docs/master/authentication) built-in, and there are [authentication libraries](http://phprbac.net/) you can use. At the absolute least follow [recommended security best practices](http://www.phptherightway.com/#security) and **never store passwords as plain-text** or a weak hash like **SHA1 or MD5**. – tadman Aug 04 '20 at 19:42
  • I don't want it to open the login screen, I want it to close the window / tab that is open with that form – Victor Maziero Aug 04 '20 at 20:01
  • This is an example script, I am studying and a lot of php, when going to production will be done by pdo – Victor Maziero Aug 04 '20 at 20:03

1 Answers1

0

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>

<script>
var myWindow;

function openWin() {
  myWindow = window.open("", "myWindow", "width=200,height=100");
  myWindow.document.write("<p>This is 'myWindow'</p>");
}

function closeWin() {
  myWindow.close();
}
</script>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>

<script>
var myWindow;

function openWin() {
  myWindow = window.open("", "myWindow", "width=200,height=100");
  myWindow.document.write("<p>This is 'myWindow'</p>");
}

function closeWin() {
  myWindow.close();
}
</script>

</body>
</html>
Hammad Ahmed khan
  • 1,618
  • 7
  • 19