I am trying to use Facebook login on my website with inserting Facebook user data on my database.
I have those files as shown below :
index.php:
session_start();
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/preload.css?ve=5"/>
<title>Myexpect</title>
</head>
<body>
<?php
if ($_SESSION['FBID']):
require_once('connections/connect.php');
?> <!-- After user login -->
<div id="warper">
<div class="logo">
<img width="150px" height="150px"src="images/Logo - Copy.png">
</div>
<div class="welcome">
<h2>Welcome to our website</h2>
</div>
<div class="preload">
<img width="50" height="50px"src="loading.gif">
</div>
</div>
<?php
echo '<meta http-equiv = "refresh" content = "3; url = games.php" />'
?>
<?php else: ?> <!-- Before login -->
<div class="container">
<div><img src="images/fb-login-btn.png" ></div>
</div>
<?php endif ?>
</body>
</html>
fbconfig.php :
<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require 'dbconfig.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxxxxxxxxxxx','xxxxxxxxxxxxxx' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('https://xxxxx/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
$check = mysqli_query($connection,"select * from Users where Fuid='$fbid'");
$check = mysqli_num_rows($check);
if (empty($check)) { // if new user . Insert a new record
$query = "INSERT INTO Users (Fuid,Ffname,Femail) VALUES ('$fbid','$fbfullname','$femail')";
mysqli_query($connection,$query);
} else { // If Returned user . update the user record
$query = "UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";
mysqli_query($connection,$query);
/* ---- header location after session ----*/
header("Location: index.php");
} } else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
The problem i have is when user try to login successfully it is not redirect to index.php file but fbconfig.php with a code :
https://xxxx/fbconfig.php?code=AQAAuanQBQ9lmXsAgbCuRB3aYy3YEEhrd81VBMMjih5oHo4dK_C7zMkQPZnuX5EdVvRJ2v4ybyAQ3EZ7qTzIrK9Oo-uY0KWiA6ZjNVh_4I6J7_AZDsJ12f7LGfc8EAGwgAfWwAm2Scwkx0UON9Vpjj75SBg7TX7n....etc
when i click back it work correctly and index.php work
how can i solve this
Sorry it was variables name mistake
this code is wrong :
$query = "UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";
i changed it to :
$query = "UPDATE Users SET Ffname='$fbfullname', Femail='$femail' where Fuid='$fbid'";
and it is work now
Related
I just want to display a Excel file in a twig view with a Symfony2.5 Controller.
With PHPExcel, I can do this trick and it works :
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once '/../Classes/PHPExcel.php';
$filename = 'filename.xlsx';
$reader = PHPExcel_IOFactory::createReaderForFile($filename);
$excel = $reader->load($filename);
$writer = PHPExcel_IOFactory::createWriter($excel, "HTML");
?>
<html>
<head>
</head>
<style>
<?php
echo $writer->generateStyles(true);
?>
</style>
<body>
<?php
echo $writer->generateSheetData();
?>
</body>
</html>
How can I do the same thing in my Controller in Symfony 2.5 ?
Actually i have this :
public function newAction()
{
$filename = 'filename.xls';
$reader = \PHPExcel_IOFactory::createReaderForFile($filename);
$excel = $reader->load($filename);
$writer = \PHPExcel_IOFactory::createWriter($excel, "HTML");
echo $writer->generateStyles();
echo $writer->generateSheetData();
}
What should i have in my render view ?
Thanks for help !
Hello guys my friend using a trick his upload cookies on his side sometime visitor click on log out button then cookies
not work his create a cookies activator please anyone tell me how its done this
i Just trying to say like this
set cookies(my target side like example.com )
how to log in on anyone using my username password using my HTML page
without knowing my account detail ....
So, first of all you need to make a page that'll set a cookie. For example consider this file named setCookie.php:
<?php
setcookie("user", "Alex Porter", time()+3600);
Now in your regular page HTML button:
<button id="cookieSetter">Set Cookie</button>
Now using jQuery send an AJAX request to setCookie.php file:
$('#cookieSetter').click(function(e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "setCookie.php.php"
});
});
<?php
// you should start the session before any html
session_start();
// Creating some session variables like this
$_SESSION['pseudo'] = 'pseudo';
$_SESSION['password'] = 'password';
setcookie('pseudo', 'whatever', time() + 365*24*3600, null, null, false, true); // Setting up a cookie
setcookie('location', 'Russia', time() + 365*24*3600, null, null, false, true); // Just another one...
// And only after that you cant write html code.
?>
<!DOCTYPE html>
<html>
<head>
<title>Your page Title</title>
</head>
<body>
<p>
Hello <?php echo $_SESSION['pseudo']; ?> !<br />
You are in (login.php).
</p>
<p>
Hey ! I remember you !<br />
Your name is <?php echo $_COOKIE['pseudo']; ?> and you are located in <?php echo $_COOKIE['location']; ?> is that true?
</p>
<p>
Log out<br />
</p>
</body>
</html>
This php file appears correct, but is returning an 'unexpected end of file' error. Is the error somewhere in the html file. I have tried putting the tags inside print in "", removed one extra white space on the php line, added white spaces between the brackets[] and quotes'' on the variable lines, and still have the same error when I press enter to send the data to the php script. Here is the html code: http://pastebin.com/Rb62pZcy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Your Feedback</title>
</head>
<body>
<?php // Script 3.3 handle_form.php
// This page receives the data from feedback.html
// It will receive: title, name, email, comments in $_POST
$title = $_POST [ 'title' ] ;
$name = $_POST [ 'name' ] ;
$email = $_POST [ 'email' ] ;
$comments = $_POST [ 'comments' ] ;
print "<p>Thank you, $title $name, for your comments.</p>"
"<p>You stated that you found this example to be '$response' and added:<br />$comments</p>"
?>
</body>
Instead of this:
print "<p>Thank you, $title $name, for your comments.</p>"
"<p>You stated that you found this example to be '$response' and added:<br />$comments</p>"
Use this:
echo "<p>Thank you, $title $name, for your comments.</p>";
echo "<p>You stated that you found this example to be '$response' and added:<br />$comments</p>";
I think you miss the ";" character.
------------------UPDATE-----------------------------
I try this code and works:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Your Feedback</title>
</head>
<body>
<?php
if (isset($_POST['email'])){
$title = $_POST['title'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
}
else
{
$title = "No title arrived";
$name = "No name arrived";
$comments = "No comments arrived";
}
//$response is not defined??
$response = "Live long and prosper";
//so i defined
echo "<p>Thank you, $title $name, for your comments.</p>";
echo "<p>You stated that you found this example to be '$response' and added:<br />$comments</p>";
?>
</body>
</html>
PS: try executing this script alone...name it "handle_form.php" and put this in the nav address bar:
localhost/your_project/handle_form.php
Works on my localhost.....if your problem continue then may be the problem is in the submit page.
Saludos.
I think the problem is in the "aif.php" file"
I am trying to fetch and display an array using PHP but the HTML element <br> is showing up in my result from the following scrip. I know I have to escape the HTML tags but I'm just not sure how! Also, any other advice on this code would be greatly appreciated (i.e. is there any redundancy or areas to improve? Thanks.
HTML
<DOCTYPE! html>
<html>
<head>
<title>The Auditors' Report: Data Entry</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header><h1>Work Station<h1></header>
<div = id="leftnav">
<h2>Select Action</h2>
Name: <input type="text" id="name">
<input type="submit" id="grab" Value="Grab">
</div>
<div id="content"></div>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="aif.js"></script>
</body>
</html>
PHP - config (filename: "config.php")
<?php
$dbhost = "localhost";
$dbname = "x";
$dbuser = "y";
$dbpass = "z";
$dsn = "mysql:host=$dbhost;dbname=$dbname";
$dbh = NULL
?>
PHP - query / resulting content (filename: "aif.php")
<?php
require "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT * FROM table1 LIMIT 0,10";
$sth = $dbh->prepare($sql);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $row['field1'],"<br>";
echo $row['field2'],"<br>";
echo $row['field3'],"<br>";
}
?>
Javascript
$('input#grab').on('click', function() {
var name = $('input#name').val();
if ($.trim(name) !='') {
$.post('aif.php', {name: name}, function(data) {
$('div#content').text(data);
});
}
});
If you want to html encode a line like this:
this is <br /> a test
into a line like this:
this is <br /> a test
Use the nifty little function described here: HTML-encoding lost when attribute read from input field
I made a simple error in the Javascript. I wanted to pass back HTML and not TEXT.
With the following change, the PHP is rendered properly:
$('div#content').text(data);
to
$('div#content').html(data);
I'm a beginner in jQuery area and I have simple question like this :
I want to load (AJAX) MySQL result in array, let's say :
$row[0] = first name
$row[1] = last name
$row[2] = phone number
I have no problem with PHP part, but I have difficulties to display each of that array content on different id. because syntax I found loads everything processed by PHP :
<script type="text/javascript">
$(document).ready(function(){
$('#mysql-result').load('ajax.php');
});
</script>
how to get 'First Name', 'Last Name' and 'Phone Number' from PHP with only one time load and still I can put the result in different . thank you.
UPDATE
I give you real example about what I need. Here's my HTML file named ajax.html :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ajax Trial</title>
</head>
<body>
<div id = "fistname"><!-- ajax result goes here --></div>
<div id = "lastname"><!-- ajax result goes here --></div>
<div id = "phonenumber"><!-- ajax result goes here --></div>
</body>
</html>
and here's my PHP file, named ajax.php :
<?php
require_once 'config-min.php';
$con = mysql_connect($DbServer,$DbUser,$DbPassword);
mysql_select_db($DbName, $con);
$result = mysql_query("SELECT FirstName, LastName, PhoneNumber FROM User WHERE ID = '201' LIMIT 1");
$row = mysql_fetch_array($result);
echo $row[0];
echo $row[1];
echo $row[2];
mysql_close($con);
?>
now, my question still same... how to get this PHP result (3 echos), load once, then displayed in those 3 different divs
I too wanted to do something like you wish to.And I did the following code and It worked. Hope this helps you too.
<script type="text/javascript" src="/jquery.js"></script>
<script>
$(document).ready(function(){
$("#form_plat").submit(function(e){
var str = $(this).serialize();
row[0]=$('#First_name').val();
row[1]=$('#last_name').val();
row[2]=$('#phonenumber').val();
$.ajax({
type: "POST",
url : 'Insert_Into.pl', //if you wish to store in database
data : {'firstname_name':row[0],'last_name': row[1],'phonenumber':row[2]},
beforeSend: function() {
console.log("hey i am here");
},
success: function(){
$('#note').html('Thank you for your submission!');
$('#note').hide();
$("#fields").fadeOut('slow');
}
});
return false;
});
});
</script>);
Have you tried adding to your php code:
echo $firstname.' '.$lastname.' '.$phonenumber;