Use php to set a mysql session variable # - php

I have a Moodle site and i would like to create a mysql trigger using the id of the logged user.
Is it possible to use php to set a custom mysql session variable ( User-Defined Variable).
My goal is to get the id of the logged user and put il in a mysql global variable in order to use that information in a mysql trigger.
Something like: SET #utilisateur = $USER->id;
I was thinking of adding a line of that kind at the end of the file index.php to catch the id of the logged user but i don't know the exact syntax
$query .= "SET #utilisateur = $USER->id;"; doesn't work
Thanks in advance for your help

Related

How does Opencart store the session_id?

I'm using opencart version 3.0.3.6. I wonder, how does the session_id created?
Correct me if I'm wrong.
When I trace the code. when user access the storefront opencart. it will trigger file catalog/controller/startup/session.php on function index(). here will run code:
$this->session->start($session_id);
this code will run function on file system/library/session.php on function start(); this will trigger code:
$this->session_id = $session_id;
as I know, that code will trigger insert session_id param throught tabel "oc_session".. but I confuse how to find the query from that code.
You can get session id using this query:
$this->session->getId();

setting enum value to 1 on link click?

can someone please help, i am trying to get the column 'privellages' (i know its spelt wrong) to update in my table 'ptb_permissions' when a link is clicked.
basically i've done this before for loads of other things and its worked fine its just this not working for some reason.
users are notified in their inbox when a user sends a request to view their pictures. and the user will have two links one to approve or one to delete the request.
if they click approve then this should update the enum colum 'privellages' from 0 to 1.
this is not working. im not getting any errors im just not getting anything happening. please can someone show me where im going wrong thanks.
Yes this is ok
contents of approve_priv_pix.php;
<?php
require_once("session.php");
require_once("functions.php");
require('_config/connection.php');
approve_pix ($_GET['picture'], $_SESSION['user_id']);
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
mysql function:
function approve_pix($picture, $user) {
global $connection;
global $_SESSION;
$query = "UPDATE ptb_permissions
SET privellages='1'
WHERE id=$picture
AND to_user_id=$user";
mysql_query($query, $connection);
}
$_GET['picture'] should be $_GET['pix']
Also double check your privellages column enum values.
Yes this is ok
Here you have pix as a key, but in approve_priv_pix.php you are taking picture id from $_GET['picture']. Suppose it should be replaced with $_GET['pix']
Also, not sure why do you have <?php echo $pix['user_id']; ?> in link code. Possibly it should be something like <?php echo $pix['picture_id']; ?>
Additionally, you code is opened to sql injections. Here:
$query = "UPDATE ptb_permissions
SET privellages='1'
WHERE id=$picture
AND to_user_id=$user";
Instead of that you should better do:
$query = "UPDATE ptb_permissions
SET privellages='1'
WHERE id=" .mysql_real_escape_string($picture) . "
AND to_user_id=" .mysql_real_escape_string($user);
More details about mysql_real_escape_string. Take a look at warning message on top of that page. mysql extension is deprecated and will be remove soon. For new projects you should better use PDO or MySQLi extensions.
Another note: global $_SESSION; is not needed at all. It is accessible form any place in PHP by default.
im not getting any errors im just not getting anything happening
To see all errors you should set error_reporting to E_ALL (in your ini file or directly in code). With this option enabled you would see all notices/warnings/errors.

Update table value on page view?

I am trying to create a PHP trigger for when a user views certain pages on my website it will update the user table in the points section.
I understand the process would work something like this
on page view > update user > where user id is (**get username from session**) > add 5 to points row
Anyone have any idea how to set up something simple like this for giving users simple points for viewing pages?
My site is using PHP and mySQL for the database.
Use cookies or session variables to keep track of the user details like the username or ID. So making a pageview trigger would be as easy as adding a mysql query at the top of every page which would update the database table for views. Kinda the same way that forums operate.
E.g
<?php
session_start();
$db_connection = mysqli_connect('host','username','password','db');
$user_id = $_SESSION['userid']; //That is asssuming that you had gotten the user id on login
mysqli_query($db_connection, 'UPDATE page_views SET views_column=views_column+1 WHERE userid=$user_id');
?>
Yes, you could do something like (if you own the page the user has to visit):
<?php
$pointsForThisSite = 5;
include "points_adder.php";
?>
While Points_adder looks whether $pointsForThisSite is defined and > 0, then adds the Points to the database as you descripbed.
Is that what you are looking for?
Create a php function and call it everytime the user enter the page.
You don't need a mysql trigger because, the action is at the webpage.
function add_points($user, $page){
//If users visits too many maybe you don't want to gave him some points.
//add points
}
and invoke the function in that pages you want to score
The most unobtrusive way to do this is with an AJAX call after the page has loaded. The call should be to an include file that performs the database update operation and returns a 204 response so that the visitor's browser doesn't wait for response content.
For an Apache server;
header('HTTP/1.0 204 No Content');
header('Content-Length: 0', true);
header('Content-Type: text/html', true);
flush();
// do the table update here

How to change language of UI for current user in Roundcube using a script

I am using Roundcube as a webmail client and need to write a script in order to change language of UI for current user.
Does anybody have any idea of how to do that? I know that there is a table users which has a field language. The problem consists on how to get current username using a script.
To change the language:
// Define $customLanguage as the result of a SQL query or session
// A valid value looks like "en_GB"
$customLanguage = $_SESSION["language"]
$rcmail = rcmail::get_instance();
$rcmail->config->set('language', $customLanguage);
$rcmail->load_language($customLanguage);
$rcmail->user->save_prefs(array("language"=>$customLanguage));
//...rest of script...

How to suppress Joomla's JUser::_load:Unable to load user with id error message?

I have a Joomla 1.6 installation that operates on two databases: joomla's database and domain-specific database, both on the same MySQL server. Some entities in the domain-specific database keeps their ownership links, i.e. stores user_id of Joomla user that created it. Some of the links are inconsistent and points to non-existent Joomla user, it is normal from domain model point of view. The problem is when I try to get user name it shows
JUser::_load:Unable to load user with id 1
Since it is OK to have such links, I need to suppress this messages. The exact code that retrieves user name is just the following:
$user_id = $ticket->getUserId();
$user =& JFactory::getUser($user_id);
Wrappig this code into ob_start() ... ob_end_clean() does not help.
Of course I could do it by hacking Joomla's internals, but is there any cleaner solution? I'm new to Joomla, so maybe there is some option in admin panel to suppress those messages?
UPDATE: Setting display_errors=>off, html_errors=>off, display_startup_errors=>off noes not help. Using PHP error suppression with # as $user =# JFactory::getUser($user_id) does not help too.
Why don't you simply do pre-check if the user exists and then run your code:
$table = JUser::getTable();
$user_id = intval( $ticket->getUserId() );
if($table->load( $user_id ))
{
$user =& JFactory::getUser($user_id);
// now you are sure user exists
} else {
// user doesn't exists
}
You can turn off the displaying of error message in the configuration:
On Tab Server, set Error Reporting to none.
(Maybe also:) On Tab System, set Debug System to No.

Categories