/    Sign up×
Community /Pin to ProfileBookmark

Using the same cURL session across several pages.

Let’s say I have a page that asks if I want to log in to some remote website by using cURL. I click the button that says “log in.” I am now on the next page. How can I access the website further by using the same cURL session on a different page? Would I just start a new cURL handle but use the same session cookies? And if I had variables such as username and password how would I transfer them to the next page?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 24.2008 — You could run your page with a PHP session, then save any cookies from the remote cURL session in your session array. Subsequent pages would check to see if the remote session cookie is in your session data, and if so send it as part of your cURL request.
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — What is a PHP session?
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — Thank you! I didn't realize how that's exactly what I need I will read up on this!
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — But I can't store the cURL resource handle. Does that matter?
Copy linkTweet thisAlerts:
@NogDogMay 24.2008 — Resources cannot be serialized, and thus can not be stored in sessions. I was suggesting that you detect and save the cookie returned by the remote session (I am, of course, assuming there is a cookie to be saved), then supplying that cookie with succeeding requests. This means adding code to your cURL transactions both for receiving cookies and sending them.
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — Oh I see, a bit complicated. Actually, cURL always loads all cookies, including sessions automatically, and since I'm the only one using the script I don't think there should be a problem. One hopefully final question: Is there a way to use an object across a session?
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — I think I will just have to recreate the object with the same parameters each time.
Copy linkTweet thisAlerts:
@chesemonkylomaauthorMay 24.2008 — yes it worked!!!!
Copy linkTweet thisAlerts:
@crunch5rJan 18.2009 — can you show me an working example?
Copy linkTweet thisAlerts:
@chesemonkylomaauthorJan 19.2009 — Okay first I have a form that submits the information in a POST format (I'm not going to go into forms and PHP, you can look them up if you don't know what they are). On the next page, declare an object of type Neopets using the parameters from the POST data.

[code=php]$bot = new Neopets($_POST['username'], $_POST['password'], $_POST['neopet']);[/code]

If it successfully logged in, I set the session variables to the POST variables. (Ignore how the opening brace has no closing brace).
[code=php]
if($bot->login())
{
$_SESSION['login'] = $_POST['login'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['neopet'] = $_POST['neopet'];

[/code]

On the next page, I can use those to continue my session.

[code=php]$bot = new Neopets($_SESSION['username'], $_SESSION['password'], $_SESSION['neopet']);[/code]

To use sessions, you have to put
[code=php]session_start();[/code] at the top of your pages by the way. Look up PHP sessions.
Copy linkTweet thisAlerts:
@crunch5rJan 20.2009 — Yes i understood.

But for example i login to page1.php and then i go to page2.php were is another form were for example i submit a comment.

How can i use the first session from page1.php without logging in again in page number 2.

I understood that i have to use cookies or something but i don't really know how to do it.

Do you have any idea?

Thanks in advance.
Copy linkTweet thisAlerts:
@chesemonkylomaauthorJan 20.2009 — Just read about PHP sessions like I said. I'm pretty sure it's what you want.
×

Success!

Help @chesemonkyloma spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...