Create Script to catch HTTP Post - php

I am using Infusionsoft to send http posts. I need a quick script written that grabs the information from the http post and captures the information. I am using iSDK and code is written however there is nothing coming from http, what am I doing wrong?
<?php
require_once('iSDK/isdk.php');
$appname = "xxxxx";
$apikey ="xxxxxxxxxxxxxxxxx";
set_time_limit(0);
ob_implicit_flush(true);
$app = new iSDK;
$contactId = intval($_GET['contactId']);
if(($appname == NULL) || ($apikey == NULL)){
echo "<b>Error</b> - make sure you fill out all the fields";
exit(0);
}
if(!$app->cfgCon('i', $appname, $apikey))
{
echo "No connected";
exit();
}
$result = $app->runAS($contactId, '123');
print_r($result);
echo "Contact Id:". $contactId;
?>

Related

php Curl failed to read response correctly in if statement, echos out fine

Hello i am doing my uni work and we are working with curl and php, i have made a script as told. the script retrieves an id from a html form then using curl pass it to a php script to update a database, this all works fine, But in my curl script i read the response from the php page which echos
OK
after updating the database, and my if statement fails to read the OK, What have i done wrong ? as i said the code works fine it just does not display the right message in the if statement after reading the response, Here's what the curl script displays if i try it, again the database is updated fine ect, just not reading the response properly however as you see in the code i echo the response to test it and it displays OK, with a space before the OK so i tried " OK" and 'OK' and again with a space but all fail to match it to the response.
error
test responce = OK
the code.
Curl code
<?php
$id = $_POST["id"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://southamptonstudentrooms.com/Wad/downloadwebservice3.php");
$dataToPost = array ("id" => $id);
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
curl_setopt($connection,CURLOPT_POSTFIELDS,$dataToPost);
$response = curl_exec($connection);
// display any errors
if(curl_errno($connection)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($connection);
if ($response == " OK") {
echo "updated";
}
else if ($response == " ERROR") {
echo "updated failed";
}
else {
echo "error";
}
echo "<br><br>test response =" . $response; // displays " OK"
var_dump($response); // displays: string(4) " OK"
?>
PhP script that it access -user info to connect changed for security
<?php
$servername = "localhost";
$username = "south609_edwin";
$password = "Zx10r2006";
$dbname = "south609_test_database";
$ID = $_POST["id"];
$status;
// 11: Chris Palmer, username ChrisPalmer, Password tree987, Balance 2.00
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ht_users WHERE username='ChrisPalmer'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if($row["balance"] >= 0.79)
{
mysqli_close($conn);
try {
//WAI
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE wadsongs SET downloads=downloads + 1 WHERE songid='$ID'";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
$status = "OK";
// echo a message to say the UPDATE succeeded
// echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
else{
$status = "ERROR";
}
}
}
else {
echo "0 results";
mysqli_close($conn);
}
echo $status;
?>
The problem your having is the response that your getting is have a white spaces which is not matching with "OK" hence you can trim the white spaces with the help of trim() function in PHP
So now your code will become
if(trim($response) == "OK"){
//Your success code
}
And it was as simple as that, thank you Channaveer Hakari. Even though i was trying to read the space and string it still failed oddly, but your trim idea worked perfectly it is now working as intended. If any one knows why did the php add the space is it like C where it adds a null pointer /0 when passing it over in cases such as char streams.

php simple login not working on server side

My php login code working properly in my local machine, but its give blank page in login-post.php on server side.
I am working almost a week on this issue. Now i post question here. Some points that i already tried:
1- already use try and catch black in my code
2- I have no .htaccess file on my server, so no issue in it.
3- Give 777 permission to all files first i use ajax call, then convert it to form submission.
4- echo after each line, so that i can identify the error place, but not a single line echo. Even my first echo is at the start of file.
5- No database issue because application sign-up is working proper, though that sign-up is similar with login functionality.
Note : This code is working properly for my local server, but not working in server side.
My code is :
<?php
session_start();
$message = '';
try{
$userName = trim($_POST['username']);
$password = trim($_POST['password']);
if (empty($userName) || empty($password)) {
$message = "<p class='error'>Required Parameter is missing</p>";
} else {
include "database_access.php";
if (!$connection) {
$message = "<p class='error'>Connection Failed.</p>";
} else {
$stmt = $connection->prepare('SELECT name, username FROM users WHERE username = :username AND password = :password');
$stmt->execute(['username' => $userName, 'password' => hash('sha512', $password)]);
$user = $stmt->fetch();
if (empty($user)) {
$message = "<p class='error'>User Name or Password is incorrect</p>";
} else {
$_SESSION['logged_in'] = $userName;
header('Location: welcome.php');
exit();
}
}
}
}catch (Exception $e) {
$message = "<p class='error'>Error : " . $e->getMessage() . "</p>";
}
$_SESSION['login-error'] = $message;
header('Location: login-get.php')
?>
and login-get.php form tag is here:
<form id="login-form" method="post" action="login-post.php" class="form-horizontal form-login">
</form>

Twitter oAuth Connection issue

I am doing Login with twitter in my application and using Twitter oAuth. I am placing proper consumer key and proper consumer secret key and valid callback url still having a error
Could not connect to Twitter. Refresh the page or try again later.
so what should I do now. couldn't trace out what is causing the trouble.
My index file
<?php
/**
* User has successfully authenticated with Twitter. Access tokens saved to session and DB.
*/
/* Load required lib files. */
session_start();
require_once('oauth/twitteroauth.php');
require_once('twitter_class.php');
if(isset($_GET['connect']) && $_GET['connect'] == 'twitter'){
$objTwitterApi = new TwitterLoginAPI;
$return = $objTwitterApi->login_twitter($_GET['connect']);
if($return['error']){
echo $return['error'];
}else{
header('location:'.$return['url']);
exit;
}
}
?>
My callback.php
<?php
session_start();
require_once('oauth/twitteroauth.php');
require_once('twitter_class.php');
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
$_SESSION['oauth_status'] = 'oldtoken';
header('Location: destroy.php');
}else{
$objTwitterApi = new TwitterLoginAPI;
$connection = $objTwitterApi->twitter_callback();
if( $connection == 'connected'){
header('Location: index.php?connected=Y');
exit;
}else{
header('Location: index.php?connected=F');
exit;
}
}
Help me on this hence i am new to this couldn’t trace out the actual problem.
PS: in oAuth there is always a blank response if curl

cross-origin request blocked with extra header

I am creating a apps with sql stuffs and i am using a online database, everything works fine if i input this header("Access-Control-Allow-Origin: *");
But the next few lines i need this header as well header('Content-Type', 'text/plain');
Once i insert that and run my html and to run this particular php file, i will get this error
error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.gamestopica.net/andrew/login.php. This can be fixed by moving the resource to the same domain or enabling CORS.
anyone know what i should do to fit those 2 header under the same php file without the cross-origin error?
my php file
<?php
header("Access-Control-Allow-Origin: *");
include_once('db.php');
session_start();
header('Content-Type', 'text/plain');
$username = $_POST['username'];
$password = $_POST['password'];
$verify = 0;
$result = $db->query("SELECT * FROM `userdetails` WHERE `username` = '".$username."'")
or die("fail");
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_array($result);
$passwordDB = $row["Password"];
if($password == $passwordDB)
{
$_SESSION['user'] = $username;
$result2 = json_encode(
array("type"=>"true", "username"=>$_SESSION['user'])
);
echo $result2;
}
else
{
$result2 = json_encode(
array("type"=>"false")
);
echo $result2;
}
}
else
{
$result2 = json_encode(
array("type"=>"nothing")
);
echo $result2;
}
?>
Your call to header() is not valid syntax for that function. Try changing:
header('Content-Type', 'text/plain');
to
header('Content-Type: text/plain');
and see if that fixes your issue.

Why POST['submit'] is set when I reload?

My application is a simple login page. When it fails, I print an error message. My question is, why when I reload the page the message is been printed again? How can I fix that?
The code is working fine, I've made another php file executing the database check & connection.
<?php
require_once("include/database.php");
if(isset($_POST['submit'])) {
connect_bookstore(); // custom function
$spassword = sha1($_POST['password']);
$username = $_POST['username'];
if ( checkpassword($username,$spassword) ) { //custom function
header('Location:insert.php');
exit;
} else {
$message = "Login failed!";
}
}
?>
Inside the html body.
<?php
if (isset($message)) {
echo $message;
}
?>
<?php
session_start();
require_once("include/database.php");
if(isset($_POST['submit'])) {
connect_bookstore(); // custom function
$spassword = sha1($_POST['password']);
$username = $_POST['username'];
if ( checkpassword($username,$spassword) ) { //custom function
header('Location:insert.php');
exit;
} else {
$_SESSION['message'] = "Login failed!";
header('location: /yourfile.php');
exit;
}
}
if(isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
Fundamentally, yes, post/redirect/get... but sometimes a simple explanation is better.
I use sessions to store flash messages, then display them like this.
Thats because you are resending the same POST data when you refresh, if you do a GET request you will notice in the URL your parameters that you are passing are there, so if you refresh those parameters are once again sent. Same thing with POST.
When you reload the page, the browser will send the same request that it sent for theoriginal page.
You want a POST-Redirect-GET.

Categories