I tried running this code which creates a SESSION variable for the Google email but for some reason the $_SESSION['email'] does not want to store the data. I run the code and the only output I get after signing in with google is
hello world 1
hello world 2
<?php
session_start();
require 'openid.php';
$openid = new LightOpenID('http://www.splunk.com');
if (!$openid->mode)
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
else
{
if ($openid->validate())
{
//echo "Hello World!";
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
$_SESSION['email'] = $google_email;
echo $google_email."<br />";
echo "<pre>" . print_r($_GET, true) . "</pre>";
echo $_GET['contact_email'];
header("Location: " . google_login.php);
}
else
{
echo '<p>hello world 1</p>';
echo "<p>". $_SESSION['email']. "</p>";
echo '<p>hello world 2</p>';
}
}
?>
I modified the above example with the google example from the LightOpenID site but now it doesn't validate for some reason.
<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try
{
# Change 'localhost' to your domain name.
$openid = new LightOpenID('http://www.splunk.com');
if(!$openid->mode)
{
if(isset($_GET['login']))
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
}
else if($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
}
else
{
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
echo $google_email;
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
Related
There may be better ways of doing this, but I'm pretty sure I should still get my session_id() passed to the second page. Instead, calling this on page 2 as my first few lines generates a brand new session_id() than the one created on page 1.
PAGE 2 BEGINNING:
<?php
error_reporting(E_ALL);ini_set('display_errors',1);
session_start();
echo session_id();
$sessionid = session_id();
echo "sessionNUM          = $sessionid\n";
echo "<br>";
echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';
PAGE 1 ALL:
<?php
error_reporting(E_ALL);ini_set('display_errors',1);
session_start();
$sessionid = session_id();
$currentDate = date('Y-m-d');
//echo "sessionNUM          = $sessionid\n";
//echo "<br>";
//echo "<br>";
//echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';
session_unset();
$old_sessid = session_id();
session_regenerate_id();
$new_sessid = session_id();
session_id($old_sessid);
session_destroy();
session_regenerate_id(FALSE);
session_start();
echo session_id();
echo "<br>";
$sessionid = session_id();
$currentDate = date('Y-m-d');
echo "sessionNUM          = $sessionid\n";
echo "<br>";
echo "<br>";
echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';
//Connect to DB
require_once 'configPOS.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
//Enter Session ID and set Order ID
//search for session info already exsiting
$result=$conn->query("SELECT * FROM POS_HEADERS WHERE sessionid='$sessionid' AND date='$currentDate'");
echo mysql_error();
if(mysqli_num_rows($result) > 0){
echo "session info already exists";
}
else{
echo "test.";
/*
$sessionid = session_id();
*/
$sql="INSERT INTO POS_HEADERS (sessionid, date)
VALUES('$sessionid', '$currentDate')";
if ($conn->query($sql) === TRUE) {
echo "New order started.<br>";
}
else {
echo "Error " . $sql . "<br>" . $conn->error;
}
}
$res=$conn->query("select ORDID from POS_HEADERS where sessionid='$sessionid'");
list($ORDERNUM)= $res->fetch_row();
//echo "<br>";
echo "<br>";
echo "ORDERNUM          = $ORDERNUM\n";
$_SESSION["OrderNum"] = $ORDERNUM;
echo "<br>";
//echo "<br>";
echo "Session variables are set.";
$res=$conn->query("select ORDID from POS_HEADERS where sessionid='$sessionid'");
list($ORDERNUM)= $res->fetch_row();
echo "ORDERNUM= $ORDERNUM\n";
//echo "<br>";
echo $_SESSION["OrderNum"];
$ordernum= $ORDERNUM;
echo $ordernum;
echo '<br>';
$LOCATION = $_POST["pickedlocation"];
echo $LOCATION;
echo "<br>";
echo "<br>";
echo "END Debugging Info";
echo "<br>";
echo "<br>";
echo '<pre>' .print_r($_SESSION, TRUE) . '</pre>';
echo "<br>";
echo session_id();
echo "<br>";
?>
<input type="button" value="Home" class="homebutton" id="btnHome"
onClick="window.location = 'https://www.example.com/POS/home.php'" />
My original problem was
PAGE 2 having one session_id(), lets call it "A",
PAGE 3 had a previous session(id), lets call it "B",
PAGE 4 had session_id() "A" again, and
PAGE 5 had session_id() "B" again.
All pages 2-5 do are insert data into a database for that session_id(), but for some reason PAGE 2 kept creating a new session_id() after PAGE 1 created one, which has got me to where I am now.
Things I have discovered - The session_save_path() is writeable, the url path is identical up to the page (...). Also, on the same URL there are very similar pages in a different directory that work flawlessly, so I think it's a code error, not a php.ini error.
First, Is your browser rejecting a cookie? In that case, you may always get a new session id. Different browsers have different options/panel to accept a cookie.
Second, edit the following fields in php.ini to suitable values.
session.use_cookies
session.save_path
session.use_trans_sid
Two pages have different session_start() calls? If you want to use one session_id for all your pages you should use only one for session_start() call for both of that files.
For example, create the third page let's name it bootstrap.php and include it at the beginning of the PAGE1 and PAGE2. In this file, you can start your session.
Hope I understand your problem correctly.
<?php
error_reporting(0);
include("../../php/open.php");// db-connection
print_r($_REQUEST);
if(isset($_REQUEST['form1']))
{
if(!empty($_POST['description'])){
$id=$_REQUEST['id'];
$field_name = $_REQUEST['field_name'];
$paragraph1=$_REQUEST['description'];
$text = $paragraph1;
$query ="UPDATE website_data SET field_value = '$text', field_name= '$field_name' WHERE id = '$id';";
$result=mysql_query($query);
if($result==true){
$url="index.php?page=content_edit";
die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
}
}
else{
echo "<script>alert('page body can not be empty')</script>";
}
}
?>
and shows the output....
Alternatively you can use php header function to redirect, but don't forget to remove print_r from the top.
instead of
die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
use
header('location : '.$url);
please use this:
if($result==true){
echo "<script type='text/javascript'>window.location.href='index.php?page=content_edit'
</script>";
}
This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to let people log in with steam, using this code:
<?php
require './includes/lightopenid/openid.php';
$_STEAMAPI = "MyKey";
try {
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid/?l=english';
header('Location: ' . $openid->authUrl());
} else {
echo "<h2>Connect to Steam</h2>";
echo "<form action='?login' method='post'>";
echo "<input type='image' src='http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png'>";
echo "</form>";
}
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
echo "User is logged in.\n";
$id = $openid->identity;
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$matches[1]";
$json_object= file_get_contents($url);
$json_decoded = json_decode($json_object);
foreach ($json_decoded->response->players as $player) {
}
} else {
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
?>
I get this error:
PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38
I can't find the issue! I know where the problem is, but can't find anything wrong with what I've done. What's the cause, and how do I fix it?
You forgot one "\" before "/)" on your pattern :
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
So php is thinking that "/(" is a modifier : http://php.net/manual/sr/reference.pcre.pattern.modifiers.php
I have uploaded this code for solving facebook login problem. there're 3 files to implement this.. (login.php/playground_fb.php/member_index_fb.php) ... First you can find the simple button for href to playground_fb.php. and then there's some algorithm in playground_fb.php and then finally if user accept this app, then go to the member_index_fb.php with info sessions such as email address, first name, last name... I have searched a lot about these problems, but I only found the advice like input the session_start or stop the sessions or keep in 'www' like this... But it still remain unchanged... Who else help me please?
<!-- login.php -->
<div onclick="facebookLogin()" style="cursor: pointer;" align="center">
<img src="../build/img/login/login_facebook.png">
</div>
function facebookLogin() {
top.location.href = 'http://www.peeknchews.com/playground_fb.php';
}
<!-- playground_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '257184091349723',
'app_secret' => '6c079349483a1bc959ce515df4e8138a',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
if(isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
}
else {
$accessToken = $helper->getAccessToken();
}
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
//When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
//When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if(!isset($accessToken)) {
$permissions = ['email']; // optional
$loginUrl = $helper->getLoginUrl('http://'.$_SERVER['SERVER_NAME'].'/playground_fb.php', $permissions);
// die($_SESSION['FBRLH_' . 'state']);
echo "<script> top.location.href = '$loginUrl'; </script>";
// $permissions = ['email']; // optional
// $loginUrl = $helper->getLoginUrl('http://www.peeknchews.com/playground_fb.php', $permissions);
// echo 'LOG IN WITH FACEBOOK!';
}
else {
if(isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
else {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short lived access token for a long lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
try {
$response = $fb->get('/me?fields=email,first_name,last_name,name');
$userNode = $response->getGraphUser();
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returne d an error: ' . $e->getMessage();
unset($_SESSION['facebook_access_token']);
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returend an error: ' . $e->getMessage();
exit;
}
$fbId = $userNode->getId();
$fbEmail = $userNode->getProperty('email');
$fbName = $userNode->getName();
$fbFirstName = $userNode->getProperty('first_name');
$fbLastName = $userNode->getProperty('last_name');
$_SESSION['fbId'] = $fbId;
$_SESSION['fbEmail'] = $fbEmail;
$_SESSION['fbName'] = $fbName;
echo 'Name: ' . $fbName;
echo "<br>";
echo 'User ID: ' . $fbId . '<br>';
echo 'Email: ' . $fbEmail . '<br><br>';
echo 'Firstname : ' . $fbFirstName . '<br><br>';
echo 'Lastname : ' . $fbLastName . '<br><br>';
$image = 'https://graph.facebook.com/' . $fbId . '/picture?width=100';
echo "Picture<br><br>";
echo "<img src='$image' /><br><br> ";
//Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
echo "<script>top.location.href = 'http://www.peeknchews.com/member_index_fb.php';</script>";
}
?>
<!-- member_index_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$sessionEmail=$_SESSION['user_email'];
//facebook
$fbSessionId = $_SESSION['fbId'];
$fbSessionEmail = $_SESSION['fbEmail'];
$fbSessionName = $_SESSION['fbName'];
require_once('/home/kukkim/peeknchews.com/config.php');
?>
<li>
<a class="nav-login fancybox fancybox.iframe" href="php_login/login.php"><img src="build/img/icons/login.png">
<?php
echo "<br>";
echo $fbSessionName . "<br>";
echo $fbSessionEmail . "<br>";
$image = 'https://graph.facebook.com/' . $fbSessionId . '/picture?width=100';
echo "<img src='$image' /><br><br> ";
?>
</a>
</li>
I've searched and tried a couple of different ways but I always get a 0000-00-00 00:00:00
Here's my page:
<?php
require_once "../../maincore.php";
require_once THEMES."templates/header.php";
$earnedpoints = false;
$account = $_GET['account'];
$account = mysql_real_escape_string($account);
if ($account == "") {
echo 'Enter an account name!';
exit();
}
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$query = mysql_query("SELECT *, SUM(`times`) as amount FROM votingrecords WHERE account='$account' OR ip='$ip'");
$lasttime = mysql_fetch_array($query);
$amount = $lasttime['amount'];
$insertnew = false;
if ($amount == "") {
$insertnew = true;
}
$timecalc = $time - $lasttime['date'];
if (!$insertnew) {
if ($timecalc < 43200) {
require_once THEMES."templates/header.php";
add_to_title(" - Vote");
opentable("Error");
echo "<table class='tbl-border' width='100%' cellspacing='1' cellpadding='5'><td class='tbl2'>";
echo ' Hello '. $account .' you have already voted with this account ('. $account .') or IP ('. $ip .') in the last 12 hours!';
echo ' Last voted on: '. date('Y-m-d H:i:s', $lasttime['date']) .'';
echo '<html>';
echo '<head>';
echo '<meta HTTP-EQUIV="REFRESH" content="10; url=\vote.php?account=' . $_GET['account'] . '">';
echo '</head>';
echo '<body>';
echo '<br /><br /><center>You will be redirected to the main website in 10 seconds.</center>';
echo '</body>';
echo '</html>';
echo "</td></table>";
closetable();
require_once THEMES."templates/footer.php";
exit();
} else {
$update = mysql_query("UPDATE votingrecords SET account='$account', date='NOW()', times=times+1 WHERE ip='$ip'");
if (!$update) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $update;
die($message);
} else {
$earnedpoints = true;
}
}
} else {
$success = mysql_query("INSERT INTO votingrecords (`account`, `ip`, `date`, `times`) VALUES ('$account', '$ip', 'NOW()', 1)");
if (!$success) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $success;
die($message);
} else {
$earnedpoints = true;
}
}
if ($earnedpoints) {
$points = mysql_query("UPDATE user SET votingpoints = votingpoints + 1 WHERE login='$account'");
if (!$points) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_close();
echo '<html>';
echo '<head>';
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=vote here">';
echo '</head>';
echo '</html>';
} else {
echo 'There was an error processing your request.';
exit();
}
require_once THEMES."templates/footer.php";
?>
That solved my problem with it not inserting the date, that works fine now!! thanks alot!
I thought it would solve my issue with it not keeping track of when a user votes so they can only vote each 12 hours.It still lets people vote over and over again >.<
Is there anyway to bump this? Lmao.. Still could use some help
Don't put NOW() inside of single quotes...
I just checked the code and you have 'NOW()' in the insert take out quotes and use just now().
'NOW()' will be treated as string and while adding to mysql datetime field its invalid and thus will have 0000-00.....