Solved

example to test curl

Posted on 2013-02-04
5
510 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 83

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

15 Experts available now in Live!

Get 1:1 Help Now