Wordpress login with Webservice (AD)

Asked

Viewed 70 times

0

I have a PHP page that connects to a Webservice and performs user validation in AD.

I would like this page to be called when typed the URL of the site made in Wordpress, because in it I create cookies:

<?php
if(isset($_POST['login']) && isset($_POST['senha'])) {
if (empty($_POST['login']) || empty($_POST['senha']) ) {
	$error = 'Preencha todos os campos.';
} else {
	$client = new SoapClient('http://endereco-do-webservices/servico.asmx?WSDL', array('login'=> "usuario-servico",'senha'=> "******"));
    $vars = $client->Autentica(array('login' => stripslashes($_POST['login']), 'senha' => $_POST['senha']));
    if($vars->AutenticaResult == true) {
		$var = $_POST['login'];
        $dominio = preg_split('/\\\/', $var, -1, PREG_SPLIT_NO_EMPTY);
        if($dominio[0]==$var){
            $dominio[0] = 'meu-dominio';
            $dominio[1] = $var;
        }
		$search = $client->BuscaLogin(array('dominio'=>$dominio[0],'login' => $dominio[1]));
		$email = $search->BuscaLoginResult->Mail;
		$nome = $search->BuscaLoginResult->Nome;
		$dominiofin = $search->BuscaLoginResult->Dominio;
		setcookie("MyAuth", 'true', time()+50400);
		setcookie("MyMailAuth", $email, time()+50400);
		setcookie("MyNomeAuth", $nome, time()+50400);
		setcookie("MyDominioAuth", $dominiofin, time()+50400);
		header("location: /meu-site");
	} else {
		$error = 'Login inválido';
	}
}
}
?>

 <!DOCTYPE html>
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
 	<meta content="IE=10" http-equiv="X-UA-Compatible" />
 	<title>Verifica Login</title>
 	<!--link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/auth.css"-->
 	<!--link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/less.css"-->
 	<meta name="viewport" content="width=device-width, initial-scale=1.0">
 </head>
 <body class="auth">

 <?php if($error) {
echo '<script>window.alert("'.$error.'");</script>';
} ?>
<img src="<?php bloginfo('template_url'); ?>/img/mylogo.png" alt="">
<form method="post">
	<input type="text" name="login" placeholder="Login" />
	<input type="password" name="senha" placeholder="Senha" />
	<input type="submit" value="Entrar">
</form>
 </body>
 </html>

Which are validated in the header.php of the theme:

<?php 
if(!isset($_COOKIE['AGAuth'])) {
	$url = bloginfo('template_url');
	header('Location:'.$url.'?page_id=73');
	exit();
}
?>

With linking a record from the wp_posts table to a PHP page in the theme folder.

1 answer

0

You don’t need a página for this, you can just run the code in a hook early in startup, such as after_setup_theme, for example:

// no arquivo functions.php
add_action( 'after_setup_theme', 'inicializa_cookies' );
function inicializa_cookies() {
    setcookie("MyAuth", 'true', time()+50400);
    // etc
}

In this case the code would run in 100% of the page loads, as soon as the theme is loaded.

  • Thank you very much for the reply. But I want to set cookies only when a login and passwords validated in AD are informed. That’s why I wanted to run a page that already exists and does the validation.

  • Does the AD response go to a specific URL? How does WP know that it has received this information?

  • I edited the answer and put the code of the page that makes the validation in AD. Actually the page makes and validates and works correctly. What I need is whenever I access the site the Wordpress redirect to this page that is called authenticat.php. If the user is validated to be created cookies and access granted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.