0

Estoy intentando mandar un par de variables obtenidas desde Firebase y quiero pasarlas a PHP. Aquí las estoy mandando por GET, pero me gustaría mandarla por POST o por algún método el cual no sea a través de una URL.


firebase.initializeApp(firebaseConfig);
var provider = new firebase.auth.GoogleAuthProvider();

function IngresoGoogle(){
    firebase.auth().signInWithPopup(provider).then(function(result) {
  var token = result.credential.accessToken;
  var user = result.user;
  var name = result.user.displayName;
  var correo = result.user.email;
  var foto = result.user.photoURL;
  location.href = '../login/index.php?name=' + name + '&correo=' + correo + '&foto=' + foto;
}).catch(function(error) {
  var errorCode = error.code;
        if (errorCode === 'auth/account-exist-with-diferent-credential') {
          alert('El usuario ya existe');
        }
});
}




document.getElementById('btn-Google').addEventListener('click',IngresoGoogle,false);

Recibo la variable en PHP de esta forma

include '../conexion/conexion.php';
$username= htmlentities($_GET['name']);
$userphoto = htmlentities($_GET['photo']);
$usercorreo = htmlentities($_GET['correo']);
$_SESSION['name'] = $username;
$_SESSION['photo'] = $userphoto;
$_SESSION['correo'] = $usercorreo; 

Me gustaría saber es una buena practica realizarlo de esa manera, estuve investigando y según creo el htmlentities evita que se modifique la varible mediante URL, es correcto? Desde ya muchas gracias.

1

1 respuesta 1

0

no es posible redirigir usando el metodo POST sin embargo puedes crear un formulario temporal con Javascript solo para luego realizar el envío del mismo con los datos que tú necesites. Te dejo el ejemplo con los campos que tú estas utilizando:

// JAVASCRIPT

// Creamos el formulario con el método post
var formulario = document.createElement('form');
formulario.action='http://ejemplo.com';
formulario.method='POST';

// Input del parametro "name"
var input=document.createElement('input');
input.type='hidden';
input.name='name';
input.value='John Doe';
formulario.appendChild(input);

// Input del parametro "photo"
input=document.createElement('input');
input.type='hidden';
input.name='photo';
input.value='http://lorempixel.com/400/200/sports/';
formulario.appendChild(input);

// Input del parametro "correo"
input=document.createElement('input');
input.type='hidden';
input.name='correo';
input.value='[email protected]';
formulario.appendChild(input);

document.body.appendChild(formulario);
formulario.submit();

Luego para recibirlos en PHP tendrias que hacerlo de la siguiente manera (usando de base tu código actual):

<?php

$username= htmlentities($_POST['name']);
$userphoto = htmlentities($_POST['photo']);
$usercorreo = htmlentities($_POST['correo']);
$_SESSION['name'] = $username;
$_SESSION['photo'] = $userphoto;
$_SESSION['correo'] = $usercorreo; 

Espero te sirva (•_•)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.