Skip to content

Instantly share code, notes, and snippets.

@unique1984
Created September 4, 2017 18:11
Show Gist options
  • Save unique1984/0573c8f55a584f77e9f1e03f108dc65f to your computer and use it in GitHub Desktop.
Save unique1984/0573c8f55a584f77e9f1e03f108dc65f to your computer and use it in GitHub Desktop.
CURL Login Script PHP
<?php
$username="admin";
$password="1234";
$protocol="https";
//~ our log in data
$post = array(
'username' => urlencode($username),
'password' => urlencode($password)
);
$post_string = http_build_query($post);
//~ $uri is a page it handle post login
$uri="yasinkarabulak.com/curl_login/";
$login_page="index.php";
$requested_page="test.php";
//~ we must use cookies for stay logged in
$ckfile = tempnam ("./", "CURLCOOKIE");
//~ first curl job is login and open session.
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
//~ if connected ssl is not signed you should uncomment below 2 lines and comment out other 2.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//~ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//~ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//~ ssl verify pear
curl_setopt($ch,CURLOPT_POST, count($post));
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//~ curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL, $protocol . "://" . $uri . $login_page);
$buffer = curl_exec($ch);
echo "<h3>Logged in index.php</h3>";
echo "<code>".print_r($buffer,1)."</code>";
echo "<hr>";
//~ second curl job is retrive another requested page data.
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
//~ if connected ssl is not signed you should uncomment below 2 lines and comment out other 2.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//~ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//~ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//~ ssl verify pear
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL, $protocol . "://" . $uri . $requested_page);
$buffer = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
else{
echo "<h3>Logged in test.php</h3>";
echo "<code>".print_r($buffer,1)."</code>";
}
curl_close($ch);
?>
<?php
session_start();
echo "POST DATA: <pre>".print_r($_POST,1)."</pre>";
if($_POST){
if(isset($_POST["username"]) && isset($_POST["password"])){
if($_POST["username"]=="admin" && $_POST["password"]=="1234"){
$_SESSION["login"]="admin_1234";
}else{
unset($_SESSION["login"]);
header("Location: index.php");
}
}else{
unset($_SESSION["login"]);
session_destroy();
header("Location: index.php");
}
}
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Curl Login Test</title>
</head>
<body>
<?php
if(!isset($_SESSION["login"])){
?>
<p>
<h3>This Page has post login.</h3>
<b>Username: </b> admin<br>
<b>Password: </b> 1234<br>
when login succeed we should see a pharagraph for a member area.
</p>
<p>
<form name="fr1" method="post" action="index.php">
<table>
<tr>
<td>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="admin">
</td>
</tr>
<tr>
<td>
<label for="username">Password:</label>
<input type="password" name="password" id="password" value="1234">
</td>
</tr>
<tr>
<td>
<label for="submit">Log In:</label>
<input type="submit" name="submit" id="submit" value="Log In">
</td>
</tr>
</table>
</form>
</p>
<p>
<h3>If you want to login and fetch data in this page with curl</h3>
<big><b><a href="curl_login.php">Click Curl Login Script</a></b></big><br><br>
It will login & fetch the data from member areas <big><b>index.php and <a href="test.php">test.php</a></b></big>
</p>
<?php
}elseif(isset($_SESSION["login"]) && $_SESSION["login"]=="admin_1234"){
?>
<p>
<h3>We are logged in.</h3>
You should see this paragraph. <br> Here is the member area.
</p>
<p>
<form name="fr2" method="post" action="index.php">
<table>
<tr>
<td>
<label for="submit">Log Out:</label>
<input type="submit" name="logout" id="logout" value="Log Out">
</td>
</tr>
</table>
</form>
</p>
<p>
<b>see <a href="test.php">test.php</a></b>
</p>
<?php
}
?>
</body>
</html>
<?php
session_start();
if(isset($_SESSION["login"]))
echo "LOGIN CREDENTIALS: <pre>".$_SESSION["login"]."</pre>";
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Curl Login Test</title>
</head>
<body>
<?php
if(isset($_SESSION["login"]) && $_SESSION["login"]=="admin_1234"){
?>
<p>
<h3>We are logged in.</h3>
You should see this paragraph. <br> Here is the member areas TEST page <br>
</p>
<p>
<form name="fr2" method="post" action="index.php">
<table>
<tr>
<td>
<label for="submit">Log Out:</label>
<input type="submit" name="logout" id="logout" value="Log Out">
</td>
</tr>
</table>
</form>
</p>
<?php
}else{
?>
<h3>You should login first!</h3>
<a href="index.php">Index.php</a>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment