登录到网站并从页面获取html [英] Login into a website and get html from a page

查看:174
本文介绍了登录到网站并从页面获取html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,我已经阅读了很多关于我的问题,但没有找到答案。
我必须登录到一个网站并从一个页面获取一些HTML只有登录后才能访问。我在Visual Basic使用 IE Object

I'm a little bit confused, I've read about a lot about my problem but without finding an answer. I have to login into a website and fetch some HTML from a page reachable only after logging in. I did this in the past with Visual Basic, using the IE Object and acting like a script, but this gave me a lot of problems, mostly because it's so slow.

我的网站很容易访问,只需使用POST请求,例如< url> / j_security_check?j_username = username& j_password = pass
我不知道的是如何检查我是否登录,
如何使用创建的会话访问页面
和如何获取HTML(大部分由JavaScript生成)

My website it's easy accessible just using a POST request like <url>/j_security_check?j_username=username&j_password=pass what I don't know is how to check whether or not I'm logged in, how to reach the page using the created session, and how to fetch the HTML (mostly generated by JavaScript)

我从未创建过登录表单,我不知道会话如何工作。我也很困惑需要的标题,以及服务器给出的请求和响应。

I never created a login form before, and I don't know how sessions work. I'm also confused about what the header is needed for, and what a Request and Response represents given by the server.

如果有人可以指向正确的方向。

If someone could point me in the right direction to learn these concepts I would highly appreciate that.

推荐答案

想想 SESSIONS 作为服务器内存中的变量。它们作为存储在用户计算机上的cookie保留。以下是两个简短但实用的说明:此处这里

Think of SESSIONS as variables in the server's memory. They persist as cookies stored on the user's computer. Here are two brief but helpful explanations: here and here

< a href =http://stackoverflow.com/questions/34734297/php-secure-member-only-pages-with-a-login-system/34735490#34735490>这是一个简化的代码示例 PHP中的登录系统。当登录成功或失败时,您可以(a)将用户重定向到安全页面,或(b)将其返回到公共页面重试。在PHP代码中,您可以使用 headers()方法或javascript中使用 window.location.href =webpage.html重定向它们; 。上面的例子使用js方法,它也演示了如何保护网页,使里面和一些 public

Here is a simplified code example of a login system in PHP. When a login succeeds or fails, you either (a) redirect the user to a secured page or (b) return them to a public page to try again. In PHP code, you can redirect them using the headers() method, or in javascript with window.location.href="webpage.html";. The above example uses the js method, and it also demonstrates how to secure web pages to make some pages inside and some public.

无论选择PHP方法还是javascript方法(重定向到其他页面)都取决于如何处理用户的登录/密码。如果使用HTML表单,它们通过将数据发布到辅助页面(实际导航到该页面)来处理数据,并对其进行处理。这一切都发生在PHP。

Whether you choose the PHP method or the javascript method (to redirect to a different page) depends on how you process the login/password from the user. If you use HTML forms, they work by POSTing the data to a secondary page -- actually navigating to that other page -- processing the data, and doing something with it. This can all happen in PHP.

最常见的方法是保持在同一页面上(不离开它),只发送数据到辅助PHP页。该页面接收用户数据(id / pw),将这些凭据与您在数据库中存储的内容(或者甚至仅仅是该非常PHP文件中的变量)和 ECHOs 回到登录页面。在 success:函数中接收到响应,然后使用javascript代码将用户重定向到内页。

The most common method these days involves remaining on the same page (not navigating away from it) and sending only the data to a secondary PHP page. That page receives the user data (id/pw), compares these credentials to what you have stored in a database (or even just to a variable inside that very PHP file), and ECHOs a response back to the login page. The response is received inside a success: function, and you then use the javascript code to redirect the user to an inside page.

在保留在原始页面上的同时将数据发送/接收到辅助PHP页面称为AJAX。这很简单。 以下是一些简单的概述示例。我敦促您将代码复制到您的服务器,并使示例工作 - 更改几个事情,看看每个工作原理。

Sending / receiving data to a secondary PHP page while remaining on the original page is called AJAX. It's pretty simple. Here is a brief overview with some simple examples. I urge you to copy the code to your server and make the examples work - change a few things to see how each one works.

注意,有两种方法可以将数据从一个网页发送到另一个网页:GET和POST。最明显的区别是,GET方法通过将变量/值附加到URL,如您在问题中所显示的那样工作:

Note that there are two ways to send data from one web page to another: GET and POST. The most obvious difference is that the GET method works by appending variables/values to the URL, as you displayed in your question:

<url>/j_security_check?j_username=username&j_password=pass

POST方法更隐藏 - - 您需要使用开发人员工具查看数据,因此它是首选。

The POST method is more hidden -- you need to use developer tools to see the data -- so it is preferred.

GET和POST源自HTML表单,大多数人立即关联这两个。在这些现代的AJAX中,根本不需要< form> 标签。事实上,如果您使用AJAX使用< form>< / form> 结构,您必须禁止导航到辅助页面的默认操作:

GET and POST originated with HTML forms, and most people immediately associate the two. In these modern days of AJAX, there is no need for <form> tags at all. In fact, if you use a <form></form> structure with AJAX you must suppress their default action of navigating to the secondary page:

<form id="myForm" action="anotherpage.php" method="GET">
</form>

$('#myForm').submit(function(event){
    event.preventDefault(); //suppress default form action
    //Do some stuff
    $.ajax({
        type: 'post', //this is where you now do the GET or POST
         url: 'my_secondary_file.php',
        data: 'varname=' + field_value_variable + '&nuthervar=' +nutherval,
        success: function(d){
            if (d == 'whatever you echo from php') window.location.href = 'my_secret_page.php'
        }
    });
});

这篇关于登录到网站并从页面获取html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆