Unable to alert before redirecting to another php script using header() - header

I was trying to create a dummy social networking site for practice. I'm stuck in a script which is sending friend request..
i've created a form(with a single text input, a hidden input and submit button) in which a logged in user can enter email address of a friend. When he clicks on the search button, he gets name and email of his friend(fetched using sql)..
With that i have added an anchor tag(named as Send request) displayed near the details. When a user clicks on the anchor tag, he is redirected to a new script which sends friend request to his friend. The second script only deals with mysql.
On sql completion, a user either gets "Friend Request Sent Successfully" alertbox or "Friend Request Already Sent" alertbox based on sql queries. Now i want to redirect the user to the first script after the execution of alertbox.
I tried to use header("Location: send_request.php") after the alertbox. But now it is redirecting user to the first script without showing alertbox.
Here is my first script(named as find_friends.php)
<?php
if(isset($_SESSION)) session_start();
require('header.php');
echo "<form method='POST' action=''>";
echo "<input type='text' name='search_user'/>";
echo "<input type='hidden' name='search_form_status' value='sent'/>";
echo "<input type='submit' value='Search User'/>";
echo "</form>";
if(isset($_POST['search_form_status']) && $_POST['search_form_status']=='sent'){
$search_user = $_POST['search_user'];
$sql_search_user = "SELECT id, name, email FROM registration where email=\"$search_user\"";
$query_search_user = mysql_query($sql_search_user);
$user_check = mysql_num_rows($query_search_user);
if($user_check==1){
$results_search_user = mysql_fetch_assoc($query_search_user);
echo $_SESSION['searched_id'] = $results_search_user['id'];
echo $_SESSION['searched_name'] = $results_search_user['name']."<br/>";
echo $_SESSION['searched_email'] = $results_search_user['email'];
echo "</br/><a href='send_request.php'>Send Request</a>";
}
}
?>
And here is second script(named as send_request.php)
<?php
$db1 = mysql_connect("localhost", "root", "");
mysql_select_db("test", $db1);
$db2 = mysql_connect("localhost", "root", "", true);
mysql_select_db("friends", $db2);
if(!isset($_SESSION)) session_start();
$searched_email = $_SESSION['searched_email'];
$searched_id = $_SESSION['searched_id'];
$user_name = $_SESSION['name'];
$user_email = $_SESSION['email'];
$sql_table_check = "SHOW TABLES LIKE 'friends_".$searched_id."'";
$table_check = mysql_num_rows(mysql_query($sql_table_check, $db2));
if($table_check==1){
$sql_insert_values = "INSERT INTO friends_".$searched_id." VALUES(\"\", \"$user_name\", \"$user_email\", 0)";
if(mysql_query($sql_insert_values)){
echo "<script>alert('Friends Request Sent Successfully')</script>";
header("Location: find_friends.php");
}
else{
echo "<script>alert('Friends Request Already Sent')</script>";
header("Location: find_friends.php");
}
}else{
$sql_create_table = "CREATE TABLE friends_".$searched_id." (friend_id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, friend_name VARCHAR(30) NOT NULL, friend_email VARCHAR(50) NOT NULL UNIQUE, active TINYINT NOT NULL)";
$sql_insert_values = "INSERT INTO friends_".$searched_id." VALUES(\"\", \"$user_name\", \"$user_email\", 0)";
mysql_query($sql_create_table, $db2);
if(mysql_query($sql_insert_values, $db2)){
echo "<script>alert('Friends Request Sent Successfully')</script>";
}
}
?>

Try this:
if(mysql_query($sql_insert_values)){
echo "<script>
alert('Friends Request Sent Successfully');
window.location.href='find_friends.php';
</script>";
}

Related

Could not find RETS data in TREB VOW Data Access

This is connection file
date_default_timezone_set("Canada/Eastern");
require_once("vendor/autoload.php");
$config = new \PHRETS\Configuration;
$config->setLoginUrl('http://retsau.torontomls.net:6103/rets-treb3pv/server/login')
->setUsername('XXXX1')
->setPassword('76XXXXX')
->setRetsVersion('1.7');
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
echo '<pre>';
var_dump($connect);
echo '</pre>';
if($connect)
{ echo "connetced";
else{
echo 'not connected';
}
After connection could not find any data
$results = $rets->Search('Property', 'A', '*', ['Limit' => 3, 'Select' => 'LIST_1,LIST_105,LIST_15,LIST_22,LIST_87,LIST_133,LIST_134']);
foreach ($results as $r) {
echo '<pre>';
var_dump($results);
echo '</pre>';
}
I need to display this data after connection
First you should check TREB metadata, find correct field names and use them in the search. Moreover, you should send a query as 3rd argument of Search function.

mysql_num_rows gives boolean error

I'm working on a login script currently and I'm trying to get comfortable with query's and all those things around it. I'm getting it slightly, but the code below, which checks the username & password when people want to login, doesn't quite work. I get an error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mystagram\logic\login_validate.php on line 13
<?php
include('../includes/config.php');
include('../includes/database.php');
$username = mysql_escape_string($_POST['username']);
$password = mysql_escape_string($_POST['password']);
$submit = $_POST['submit_login'];
if (isset($submit)) {
$loginquery = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
if (mysql_num_rows($loginquery) > 0) {
echo 'You are now logged in.';
exit();
} else {
echo 'Wrong username or password.';
}
}
?>
This means that you have an error in your SQL Statement.
Probably you dont have a table named 'users' or the Column 'username' or 'password' is missing.
Change this:
if (mysql_num_rows($loginquery) > 0) {
To:
if (mysql_num_rows($loginquery) == 1) {
And don't save your password in plaintext in the database. And use mysqli :)
you use mysql_escape_string() then '' it was problem. try this code,I hope it will work.
<?php
include('../includes/config.php');
include('../includes/database.php');
$username = mysql_escape_string($_POST['username']);
$password = mysql_escape_string($_POST['password']);
$submit = $_POST['submit_login'];
if (isset($submit)) {
**$loginquery = mysql_query("SELECT * FROM users WHERE username=".$username." AND password=".$password);**
if (mysql_num_rows($loginquery) > 0) {
echo 'You are now logged in.';
exit();
} else {
echo 'Wrong username or password.';
}
}
?>

INSERT in the query part of BigQuery, using google-api-php-client

I wanted to insert thousands of records in the database using INSERT command in php script , as it will easier to access, but https://developers.google.com/bigquery/docs/query-reference , this doesn't show the INSERT command that can be used while querying in php , like ,
$quert->setQuery("Insert into ... values...") , Have tried this in the Query Table of BigQuery web console, but it doesn't seem to work , Is there anyway to use setQuery() with some other command, to insert data ?
BigQuery doesn't support the INSERT command. You would need to create a load job. See https://developers.google.com/bigquery/docs/import#localimport for more information.
In addition to Jordan's answer, here's a snippet of code that should get you started using the Google BigQuery API and the Google API PHP client library for loading your own data into BigQuery programmatically. Note, this snippet simply spits out the raw API response of the load job, including the job Id to the screen - you'll have to add your own polling logic to check on the load job status.
(We will be adding additional documentation about loading your own data, as well as more PHP samples soon).
<?php
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_BigqueryService.php";
session_start();
$client = new Google_Client();
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setScopes(array('https://www.googleapis.com/auth/bigquery'));
$client->setClientId('XXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXX');
$client->setRedirectUri('http://YOURAPPLICATION/index.php');
// Instantiate a new BigQuery Client
$bigqueryService = new Google_BigqueryService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
?>
<!doctype html>
<html>
<head>
<title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
<div id='top'><h1>BigQuery API Sample</h1></div>
<div id='main'>
<?php
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
// Your project number, from the developers.google.com/console project you created
// when signing up for BigQuery
$project_number = 'XXXXXXXXXXXXXX';
// Information about the destination table
$destination_table = new Google_TableReference();
$destination_table->setProjectId($project_number);
$destination_table->setDatasetId('php_test');
$destination_table->setTableId('my_new_table');
// Information about the schema for your new table
$schema_fields = array();
$schema_fields[0] = new Google_TableFieldSchema();
$schema_fields[0]->setName('first');
$schema_fields[0]->setType('string');
$schema_fields[1] = new Google_TableFieldSchema();
$schema_fields[1]->setName('last');
$schema_fields[1]->setType('string');
$destination_table_schema = new Google_TableSchema();
$destination_table_schema->setFields($schema_fields);
// Set the load configuration, including source file(s) and schema
$load_configuration = new Google_JobConfigurationLoad();
$load_configuration->setSourceUris(array('gs://YOUR_GOOGLE_CLOUD_STORAGE_BUCKET/file.csv'));
$load_configuration->setDestinationTable($destination_table);
$load_configuration->setSchema($destination_table_schema);
$job_configuration = new Google_JobConfiguration();
$job_configuration->setLoad($load_configuration);
$load_job = new Google_Job();
$load_job->setKind('load');
$load_job->setConfiguration($job_configuration);
$jobs = $bigqueryService->jobs;
$response = $jobs->insert($project_number, $load_job);
echo '<pre>';
print_r($response);
echo '</pre>';
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Authorize Access to the BigQuery API</a>";
}
?>
</div>
</div>
</body>
</html>

Issue with retrieving Magento Frontend Session

I am trying to retrieve the customer's login status from Flex application using AMF call to the Magento Customer API :
Mage::app('default');
$session = Mage::getSingleton('customer/session', array('name'=>'frontend') );
$sessId= $session->getSessionId();
if($session->isLoggedIn()) {
$name = "Hi ". Mage::getModel('customer/session')->getCustomer()->getName();
return 'true' . $name;
}
else{
return 'false ' . $sessId;
}
Only the PHP session ID is returned:
PHPSESSID=i5s1gcemc6r8uquadc4rsk9ou5
But the user is logged into the below ID
frontend=3qdcimcdp7nq4bi8jlovqmnq61
Let me know if I am missing something here.
Use the following code to get the customer ID
Mage::getSingleton('core/session', array('name' => 'frontend'));
$customer = Mage::getSingleton('customer/session',array('name' => 'frontend'));
echo $customerId = $customer->getCustomer()->getId();

Best way to display current logged-on user in default.ctp?

I am in the process of customizing the default.ctp file and I am trying to display the currently logged on user's name on the top of the page.
In app_controller.php, I have the following:
function beforeFilter()
{
$user = $this->Auth->user();
if($user != null)
{
$this->Session->write('user_name',$user['User']['username']);
}
}
And in default.ctp, I have:
$user = $this->Session->read('Auth.User');
if(!empty($user))
{
echo 'Hello, ' . $user['user_name'];
}
However, it seems like the value $user_name is not getting set anywhere.
What am I doing wrong? Is there a better way to accomplish this?
Update: I've modified it as described in the answer, but it still doesn't work. I get an error:
Undefined index: user_name [APP/views/layouts/default.ctp, line 21]
you can also use the SessionHelper directly in the view / layout
$user = $this->Session->read('Auth.User');
if(!empty($user)) {
echo 'Hi ', $user['user_name'];
}
Cakephp 2.x:
<?php if (AuthComponent::user('id')): ?>
<p class="navbar-text pull-right">
Logged in as <?= AuthComponent::user('name') ?>
</p>
<?php endif; ?>
$user = $this->Session->read('Auth.User');
if(count($user))
echo $user['name'];