example to test curl

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'
LVL 1
rgb192Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
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
 
tycootCommented:
<?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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
rgb192Author Commented:
so what is an basic example of an action page that has a cookie, on successful login
0
 
rgb192Author Commented:
$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
All Courses

From novice to tech pro — start learning today.