Session variable inside a function parameter? (API CALLBACK) - php

Back story: Basically I'm trying to make a BTC api & to do this I need to basically use the ($custom inside of it) as i need to parse the variable $_SESSION['username'] onto my callback file to validate the user. I'm truly stuck here.
function getAddress($address, $callback, $_SESSION["username"], $secret)
{
$username = $_SESSION['username'];
$root_url = 'https://blockchain.info/api/receive';
$parameters = 'method=create&address=' . $address .'&callback='. urlencode($callback);
$callback = $callback.'?custom='.$username.'&secret='.$secret;
$response = file_get_contents($root_url . '?' . $parameters);
$object = json_decode($response);
return $object->input_address;
}
I know that I can't just put the variable $_SESSION['username'] there however, I'm not 100% sure how to do this: callback file:
$confirmation_level = 4;
$real_secret = 'ZzsMLGKe162CfA5EcG6j'; //Your Secret Key
$address = 'address'; //Your Bitcoin Address
$username = $_GET['custom'];
$input_address = $_GET['input_address'];
$transaction_hash = $_GET['transaction_hash'];
$input_transaction_hash = $_GET['input_transaction_hash'];
$value_in_satoshi = $_GET['value'];
$value_in_btc = $value_in_satoshi / 100000000;
//skipping to validate part
$btcpaidupdate = $odb ->prepare("UPDATE `users` SET paid=1 WHERE `username` = '$username'");
$btcpaidupdate ->execute();
So as you can see I'm completely stuck on how to parse the actual variable onto the callback api file.

Related

Session Variable working in echo but not working in SQL query

I want to pass the value of login.php variables $user_key & $user_id to another file statusdata.php for which I have used session. In statusdata.php I want to use those session variable value in sql query of statusdata.php file.
To achieve this in login.php I pass the value of variable $user_key & $user_id to session variable $_SESSION["key"] & $_SESSION["id"] respectively then in statusdata.php I call session variable and pass their value in variable $user_key & $user_id of statusdata.php
Now the problem is when using the variable $user_key & $user_id in SQL query it is not returning proper output but using the same variable in echo it is giving proper value mean session is working fine when I echo the variable but not working in SQL. I have also tried passing the session variable directly but the same thing is happening to work in echo but not in SQL.
login.php
<?php
// Start the session
session_start();
require "conn.php";
$user_key = '8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED';
$user_id = '1997';
$mysql_qry = "select * from applications where application_key = '".$user_key."' and application_id like '".$user_id."';";
$result = mysqli_query($conn, $mysql_qry);
if (mysqli_num_rows($result) > 0){
$_SESSION["key"] = ".$user_key.";
$_SESSION["id"] = ".$user_id.";
echo "Login Success";
}
else {
echo "Login Not Success";
}
?>
statusdata.php
<?php
// Start the session
session_start();
require "conn.php";
$user_key = "-1";
if (isset($_SESSION["key"])) {
$user_key = $_SESSION["key"];
}
$user_id = "-1";
if (isset($_SESSION["id"])) {
$user_id = $_SESSION["id"];
}
//creating a query
$stmt = $conn->prepare("SELECT applications.application_id, applications.applicant_name, applications.applicant_aadhaar, applications.applicant_phone, applications.subject, applications.date, applications.description, applications.chairperson_remark, applications.status, officer_accounts.name_of_officer, applications.officer_remark, applications.last_update_on
FROM applications INNER JOIN officer_accounts ON applications.account_id = officer_accounts.account_id
WHERE applications.application_id = '".$user_id."' AND applications.application_key = '".$user_key."';");
//executing the query
$stmt->execute();
//binding results to the query
$stmt->bind_result($id, $name, $aadhaar, $phone, $subject, $date, $description, $chairperson, $status, $officername, $officerremark, $lastupdate);
$applications = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['applications.application_id'] = $id;
$temp['applications.applicant_name'] = $name;
$temp['applications.applicant_aadhaar'] = $aadhaar;
$temp['applications.applicant_phone'] = $phone;
$temp['applications.subject'] = $subject;
$temp['applications.date'] = $date;
$temp['applications.description'] = $description;
$temp['applications.chairperson_remark'] = $chairperson;
$temp['applications.status'] = $status;
$temp['officer_accounts.name_of_officer'] = $officername;
$temp['applications.officer_remark'] = $officerremark;
$temp['applications.last_update_on'] = $lastupdate;
array_push($applications, $temp);
}
//displaying the result in json format
echo json_encode($applications);
// Echo session variables that were set on previous page
echo "<br>key is " . $user_key . ".<br>";
echo "id is " . $user_id . ".";
?>
Output login.php
Login Success
Output statusdata.php
[]
key is .8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED..
id is .1997..
output I want from statusdata.php (I am getting it if I use direct value in variable $user_key & $user_id not session variable from login.php)
[{"applications.application_id":1997,"applications.applicant_name":"Tanishq","applications.applicant_aadhaar":"987654321","applications.applicant_phone":"123456789","applications.subject":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.date":"2018-07-02 09:11:47","applications.description":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.chairperson_remark":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.status":1,"officer_accounts.name_of_officer":"Chayan Bansal","applications.officer_remark":"asdnjsnadnksncdnsjnvsavasdnjsnadnksncdnsjnvsav","applications.last_update_on":"2018-07-22 09:14:25"}]
key is 8C9333343C6C4222418EDB1D7C9F84D051610526085960A1732C7C3D763FFF64EC7F5220998434C896DDA243AE777D0FB213F36B9B19F7E4A244D5C993B8DFED.
id is 1997.
NOTE: I am taking the output of statusdata.php SQL query in JSON format as in the end I am extracting it in android.
Please help me I have tried everything which other similar questions are suggesting but nothing helps
Looks more like a typo situation. You have this in login.php:
$_SESSION["key"] = ".$user_key.";
$_SESSION["id"] = ".$user_id.";
Which is tainting your original values... by adding needless dots around them. Clean that up by simply assigning as so:
$_SESSION["key"] = $user_key;
$_SESSION["id"] = $user_id;
And it should begin working better.
Side note, you are using prepare, but not using bind_param. Change your SQL prepare to the following (note the ? placeholders), and add bind_param:
$stmt = $conn->prepare("SELECT applications.application_id, applications.applicant_name, applications.applicant_aadhaar, applications.applicant_phone, applications.subject, applications.date, applications.description, applications.chairperson_remark, applications.status, officer_accounts.name_of_officer, applications.officer_remark, applications.last_update_on
FROM applications INNER JOIN officer_accounts ON applications.account_id = officer_accounts.account_id
WHERE applications.application_id = ? AND applications.application_key = ?;");
$stmt->bind_param('ss',$user_id,$user_key);

How to post data using codeigniter?

How to map post data to variables
handy.php
if($this->input->method() === 'post') {
$obj = json_decode($this->input->raw_input_stream);
print_r($this->input->raw_input_stream);
print_r($obj);
$name = $obj->name;
$mobile = $obj->mobile;
$state = $obj->state;
}
My postman response is
In codeigniter If you are sending xyz = 'some_value' using post you can set that POST value in variable as
$xyz = $this->input->post('xyz');
Your code should look like:
if($this->input->post()) {
$name = $this->input->post('name');
$mobile = $this->input->post('mobile');
$state = $this->input->post('state');
}
Assuming that you have sent some data for name, mobile and state. You can clear notices at your own.

I trying to create external bank (PHP)account but i am not getting any response.Neither i am getting error nor the account is created

function stripe_bank_payment()
{
$this->load->database();
$id = $_GET['user_id'];
$acc_id = $_GET['stripe_account_id'];
$amount = $_GET['amount'];
$get = $this->db->select('*')->where(array('user_id' => $id))->get('bank_detail');
$pay = $get->result_array();
foreach ($pay as $res) {
$token = $res['token'];
}
require_once 'stripe/init.php';
\Stripe\Stripe::setApiKey("sk_test_6zdfZQ6R0Jh7EiFONJTpYaFl");
$account = \Stripe\Account::retrieve($acc_id);
$account->external_accounts->create(array("external_account" => $token));
return $account;
}
The above is my code.
I am trying to create external bank account.
user_id, stripe_account_id, and amount is given by me.
the token which is to be used is there in bank_details table.
So i am getting token from there.
And then running the code.

Sessions not being created by form?

I am trying to create a session for each field of a form so that I can call it back in on the additional information form on the next page.
<?php
session_start(); // starting the session
if (isset($_POST['Submit'])) {
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
}
?>
Using some code and after a few modifications I have the code above trying to store data that has been submitted in the form..
How can I improve this code to make it work?
If it is not working, try debugging.
If it is saving the POST, then there is not much left to do.
Set session.cookie.secure to true. You may want to set session.cookie_lifetime.
As far as security, consider if you have something worth protecting. Does it matter if someone gets a visitor's session cookie? If not, forget it.
session_set_cookie_params(0, '/', '', true, false);
session_start();
error_reporting(E_ALL); // debug
if (isset($_POST['Submit'])) {
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
} else {
echo '<h3>Session Not Saved</h3>';
}
echo htmlentities(var_export($_REQUEST, true)); // debug
echo htmlentities(var_export($_SESSION, true)); // debug
DEBUG
It should work - if not, test and debug.
Show all Warnings.
Check $_SESSION after setting.
Check Request.
$_REQUEST includes $_COOKIE which should contain a SESSION cookie.
We need to regenerate id. Try this. (Sorry for bad english)
<?php
session_start(); // starting the session
if (isset($_POST['Submit'])) {
session_regenerate_id();
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
session_write_close();
}
?>
Store your variables in an array and loop as shown below. You can use session_start(); on your next page and access them:
<?php
session_start(); // starting the session
$sessionItems = array('breakdowndate','policyinception','customername'...'telephone');
if (isset($_POST['Submit'])) {
foreach($sessionItems as $item) {
$_SESSION[$item] = $_POST[$item];
}
}
?>

Refer to a global variable doesn't work

A im writing a simple script in PHP with few files, in 'loginForm.php' I have code:
<?php
session_start();
$login = $_GET['login'];
$password = $_GET['password'];
$remember = $_GET['remember'];
include_once("login.php");
$userAccount = new UserAccount($login, $password);
$logged = $userAccount -> isLogged();
(...)
and in file 'photo.php':
global $userAccount;
$login = $userAccount -> getLogin();
what gives mi an error:
Call to a member function getLogin() on a non-object
I also tried with $GLOBALS - same result.
Global variable does not work across requests, but have file scope.
the typical use of global variables if to have a variable that is accessible across different scopes (normally functions in the same file.
For example in file1.php
<?php
$value = 1;
echo $value; // prints '1'
function f1() {
global $value;
$value++;
}
echo $value; // prints '2'
function f1() {
$value++;
}
echo $value; // prints '2'
?>
To use variables across requests, use sessions.
safest way to create sessions in php
Storing objects in PHP session
OK, now i did:
1) In AJAX-request file i create a instance of Class UserAccount, which will be store in $_SESSION array
<?php
include_once("login.php");
session_start();
$login = $_GET['login'];
$password = $_GET['password'];
$rememberMe = $_GET['remember'];
$userAccount = new UserAccount();
$userAccount -> LogIn($login, $password);
$logged = $userAccount -> isLogged();
$_SESSION['userAccountClassObject'] = serialize($userAccount);
2) In static (non AJAX-request) file 'photo.php' it works fine:
<?php
include_once("login.php");
$user = unserialize($_SESSION['userAccountClassObject']);
$login = $user -> getLogin();
3) But in other AJAX-request file - addComment.php unfortunatelly doesn't work:
<?php
$id = $_GET['id'];
$comment = $_GET['comment'];
session_start();
include("login.php");
$user = unserialize($_SESSION['userAccountClassObject']);
$login = $user -> getLogin(); // Fatal error

Categories