V
V
Vitaly2016-02-11 18:05:03
PHP
Vitaly, 2016-02-11 18:05:03

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

4 answer(s)
S
Slava Kryvel, 2016-02-11
@kryvel

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>

You can also look here: stackoverflow.com/questions/5647461/how-do-i-send-...

K
krypt3r, 2016-02-12
@krypt3r

<?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!";
}
?>

V
Vitaly, 2016-02-11
@Neolith

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?

S
Stalker_RED, 2016-02-11
@Stalker_RED

It feels like CURL doesn't work at all, it just redirects to the web service and leaves it there.

And so it is, you have declared a function, but do not call it.
Also, the redirect header('location ... ')does not send POST data, but simply "redirects and leaves there."
Can you somehow force
Can. It's worth starting with a description of what you want to do, because. I can only guess from the code you provided.
It would be even cooler to start by reading the first chapters of some textbook, but it looks like you are looking for a "shortcut".
It would also be great if you could find where you have the error log and see what's there. There at least about indefinite variables of the message should be.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question