Solved

example to test curl

Posted on 2013-02-04
5
508 Views
Last Modified: 2013-02-09
from the advice of
DaveBaldwin
who is teaching me about curl,

I am looking for a simple example that I can put on my own server to test curl

no mysql, just php hardcoded username, password
form where curl or user can enter username and password

and a cookie is set

afterwards, gettext, 'you have logged in successfully'
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:tycoot
ID: 38852820
<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);      
?>
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38852838
Maybe that's what the problem is.  You do not use the form page as the URL in curl, you use the 'action' page of the form to submit your data to.  You are using curl to simulate the action on the form page.  The form page sends the data to the 'action' page so the curl routine should too.
0
 

Author Comment

by:rgb192
ID: 38853143
so what is an basic example of an action page that has a cookie, on successful login
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38853373
Here are three pages for you to test with.  First the 'form' page, next the 'action' page, and finally the 'curl' page.  Make sure and save them under the correct file names or they won't work.

Form-curl-test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Form for curl test</title>
</head>
<body>
<h1>Form for curl test</h1>
<form action="php-action-test.php" method="post">
Name: <input type="text" name="name" value="" /><br>
Password: <input type="password" name="password" value="" /><br>
<input type="submit" name="submit" value="submit" />
</form>

</body>
</html>

Open in new window


php-action-test.php
<?php 
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// verify POST variables
if (!isset($_POST['name']))  $name = ''; else $name = $_POST['name'];
if (!isset($_POST['password']))  $password = ''; else $password = $_POST['password'];

if(($name == "username") && ($password == "password")) {
	$value = "Logged in.";
	setcookie("TestCookie", $value, time()+3600); // this sets the cookie
	$msg = 'You have logged in successfully.';
	}
else {
	$value = "Not logged in.";
	setcookie("TestCookie", $value, time()-3600); // this removes the cookie
	$msg = 'Log in failed.';
	}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>php-action-test</title>
</head>
<body>
<h1>php-action-test</h1>
<?php echo $msg; ?>

</body>
</html>

Open in new window


php-curl-test.php (you have to change the URL to the 'action' page to work on your server.)
<?php 
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.202.46.40/ee/curltest/php-action-test.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=username&password=password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec ($ch);
curl_close ($ch);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>php-curl-test</title>
</head>
<body>
<h1>php-curl-test</h1>
<div style="background-color: #ffff33;width:400px;">
<?php echo $output; ?>
</div>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38871150
$myFile = "log-php-action-test.html";
$fh = fopen($myFile, 'a');  
$output= "<br>On ".(date("D F d H:i:s",time())).' , name: '. $name . '  , password: ' . $password . ' , message: '.$msg.'<br />';
fwrite($fh, $output);  

php-action-test
I added a log file to see I logged on when I couldnt get it to work

but then I realized that you created output in the $msg


it worked for me, thanks


I have another question about curl testing / adding to see if a cookie is there to go to page3

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28025903.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now