curl, login and session maintainig

Posted on 2010-11-19
Last Modified: 2012-06-27
i have a form to which need to login and maintain session for subequent url, below is the sample form
<form name="login_form" action="" method="post">
								<input name="username" value="Username" onblur="javascript: if(this.value=='') { this.value='Username';}" onfocus="javascript: if(this.value=='Username') {this.value='';};" />
														<input type="password" name="passwd" value="Password"  onfocus="javascript: if(this.value=='Password') {this.value='';};"  />
														<input type="submit" value="Login"/>

Open in new window

Question by:rajccs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

by:Romi Kuntsman
ID: 34172462
LVL 31

Expert Comment

by:Marco Gasi
ID: 34172491
In simply put at the top:

  if (isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    //validate username and passowrd here
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;


Expert Comment

by:Romi Kuntsman
ID: 34172526
I believe that rajccs wants their page to perform a login to another page, then maintain the logged-in session for that other page. Not to allow other to login to their page.
LVL 13

Accepted Solution

dsmile earned 500 total points
ID: 34172614
Change your form action form

Below is sample dologin.php

Assume that remote host's login page accept 'username' and 'passwd' as input data
if (isset($_POST['username'])){
// create new curl session
$sessions = curl_init();

$uri = ''; //your URL here
$postData = array('username' => $_POST['username'], 'passwd' => $_POST['passwd']); //login data stored in array format. eg: array("name" => "test", "pwd" => "test")

// set options for logging in and store cookie
curl_setopt($sessions, CURLOPT_URL, $uri);
curl_setopt($sessions, CURLOPT_POST, 1);
curl_setopt($sessions, CURLOPT_POSTFIELDS, $postData);
curl_setopt($sessions, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_setopt($sessions, CURLOPT_FOLLOWLOCATION, 1); //auto redirect after logged in
curl_setopt($sessions, CURLOPT_HEADER, 1);
curl_setopt($sessions, CURLOPT_RETURNTRANSFER, 1);

//do logging in


Open in new window


Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
if statement malfunction 5 44
Do not understand error message 3 46
php hashing methods 3 50
how to block posted data except coming from specific remote ADDR 11 38
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question