Answer the question
In order to leave comments, you need to log in
How to write such a PHP function?
Good afternoon! Do not hurt me, I'm programming for "YOU")
There is an HTML authorization form. When entering a login / password, the data is transferred to an external web service for verification. The web service returns the values 1 or 0 directly to the browser (1-ok, 0-invalid password).
It is required to write an IF function that, if one is returned, would make a POST to another page and pass the verified login and password in it. Authorization occurs with my code, 1 or 0 is issued in the browser, but IF and further redirect does not occur. Tell me what's wrong? The code:
<?php
session_start();
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$txResult = curl_exec($ch);
}
if ($txResult == 1)
header("Location: http://server.com/preauth.php"); +добавить POST (username+pass)
//elseif ($txResult == 0)
// echo "Wrong usename or password!";
?>
<!DOCTYPE html>
<html>
<head>
<title>pass validation</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<section class="loginform cf">
<form name="login" action="https://webservissavtorizacii.com/check.jsp" method="get" accept-charset="utf-8">
<ul>
<li>
<label for="username">User</label>
<input type="text" name="username" placeholder="yourname" required>
</li>
<li>
<label for="password">Password</label>
<input type="password" name="password" placeholder="password" required></li>
<li>
<input type="submit" value="Login">
</li>
</ul>
</form>
</section>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
because you have $txResult as a local variable in the file_get_contents_curl function and it is undefined outside of this function.
and in general it is very strange that you define it there and then do not use it.
try like this:
<?php
session_start();
$url = "https://webservissavtorizacii.com/check.jsp"
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($_POST),
),
);
$context = stream_context_create($options);
$txResult = file_get_contents($url, false, $context);
if ($txResult == 1)
echo "Success!";
elseif ($txResult == 0)
echo "Wrong usename or password!";
?>
<!DOCTYPE html>
<html>
. . .
<!-- Здесь http://server.com/preauth.php это ваш php скрипт который мы редактируем -->
<form name="login" action="http://server.com/preauth.php" method="post" accept-charset="utf-8">
. . .
</html>
<?php
session_start();
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
return curl_exec($ch);
}
$txResult = file_get_contents_curl($url); // тут должна быть ваша ссылка на ваш внешний веб-сервис для проверки
if ($txResult == 1) {
header("Location: http://server.com/preauth.php"); /* добавить POST (username+pass) */
exit;
} elseif ($txResult == 0) {
echo "Wrong usename or password!";
}
?>
Without changes. Throws on the page with a unit and that's it. It feels like CURL doesn't work at all, it just redirects to the web service and leaves it there.
Maybe you can somehow force the HTML form to be posted in PHP, and then post with CURL, without going to the web service, and then set the IF?
It feels like CURL doesn't work at all, it just redirects to the web service and leaves it there.
header('location ... ')
does not send POST data, but simply "redirects and leaves there."Can you somehow forceCan. It's worth starting with a description of what you want to do, because. I can only guess from the code you provided.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question