I've been following the Microsoft documentation trying to set a session using this line
HttpContext.Session.SetString(SessionKeyName, "Rick");
from the page
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?tabs=aspnetcore2x
but it gives the error
'ISession' does not contain a definition for 'SetString'
There's a Set option, but that takes a byte.
Any idea what I'm doing wrong and why I'm using the wrong HttpContext.Session?
SetString,GetString,GetInt32 and SetInt32 are extension methods on ISession defined inside the Microsoft.AspNetCore.Http namespace. So to use these, you should add a using statement to this namespace in your class.
using Microsoft.AspNetCore.Http;
Now in your class, you can use these extension methods
HttpContext.Session.SetString("Test", "Rick");
HttpContext.Session.SetInt32("Age", 25);
Related
What does the E2E_CONTEXT namespace in Oracle's implementation of the JDBC 4.1 standard mean? Where is it defined? Where can I find all defined namespaces? Can I define my own namespaces and if so, how?
The namespace E2E_CONTEXT is now internal to Oracle and shouldn't be used. It was created to support end-to-end tracing but was quickly replaced by the more complete OCSID namespace which can be used to set the following attributes:
ACTION
MODULE
CLIENTID
ECID
DBOP
using the JDBC standard setClientInfo method. These attributes are then propagated to views like V$MYSTAT, V$SESSION, V$SQL, etc. There is another built-in namespace called CLIENTCONTEXT that can be used to store custom key value pairs (see example below).
You can't use your own namespace through Oracle JDBC.
con.setClientInfo('OCSID.CLIENTID','my Client ID');
con.setClientInfo('OCSID.MODULE','my Module');
con.setClientInfo('OCSID.ACTION','my Action');
con.setClientInfo('CLIENTCONTEXT.MyKey', 'MyValue);
We had Forify scan for our website code and fortify reported few issues. We are using CodeIgniter 3.1.9 framework. One of the issue they mentioned as
Possible Variable Overwrite' for function 'extract()' in file mysqli_utility.php.
As this is core file of CI framework, I'm not using this function directly and also I do not know where this function getting used by CI.
Will you please help to resolve the issue reported by Fortify? What could be the solution?
extract() imports variables from an array into the current symbol table. The phrase "current symbol table" basically means into the current scope of the code. For the usage in question, extract() is called inside a class method. So the current scope for the extracted vars will be in that method and that method only for that instance of the class.
CodeIgniter's core code does not define or use global variables. Unless code developed for the application uses globals (which it should not as "globals" are more of a "procedural programming" thing) the possibility of overwriting is exceedingly low.
I'm curious as to why the scan didn't pick up all the other times extract is used by CodeIgniter.
I found that some developer use app(SomeService::class); while other use new SomeService(); in Laravel? Is there any difference between them?
Yes, the main difference is the ServiceContainer.
If you instantiate using app(YourService::class), the ServiceContainer will use reflection to inject in the class constructor the dependecies required.
So you don't have to explicit use all the dependencies needed.
It's well explained here.
Please check https://laravel.com/docs/5.7/providers.
Briefly speaking if you want to customize the class which you will use in runtime you can change it in provider (make it singleton or pass some arguments) and get in runtime via $app (if you have no opportunity to use DI). but when you making object vie new its only creates an instance.
Imagine that we have class A which receive 2 config parameters in construction.
So you need everywhere call new A($param1,$param2) . but using providers u can use DI to get instance of class A with already passed parameters or $app if u have no opportunity to use DI
I'm trying to include Crypt/RSA.php in my model, but this always appear "AppClass 'App\Crypt_RSA' not found". Any idea? Thanks in advance
That error appears because you're trying to instantiate a the Crypt_RSA class in the App namespace where it doesn't exist, because the Crypt_RSA class is defined in the global namespace. So you can do one of two things:
1. Write a use statement at the top of your file:
use Crypt_RSA;
2. Prepend a backslash to the class name when using it:
new \Crypt_RSA();
You can read more on how namespaces work in the PHP Namespaces Documentation.
I'm updating an php web application that is becoming multilingual, based on the Zend MVC framework, and I'm trying to figure out the best approach to passing the translation object to different classes/layers.
Most of my translation is done at the View level, but there are a few cases where I need to return status messages from custom libraries.
I could just create a property for the library and set the translator, but I'm wondering if there is a better way to integrate a translator object into an existing application?
Hold the users lanaguage in a Memento, and pass that through the program logic, when you need to do that translation use it identify the language.
If using Zend_Translate, it's best option to use register.
Zend_Registry::set('Zend_Translate', $translate);
This way all classes can find it automatically (Zend_Form, Zend_Validate, ...)
You could always instantiate the translator in the bootstrap.php so it is available to all classes as a global. That's how I'd do it since you probably need to use it everywhere. It isn't elegant, but it keeps you from having to add code wherever a class needs to throw an exception or return an error message.
If you don't have that many controllers setup can you not extend the base controller and instantiate the translator there? It should be available for use throughout the system then.
Something like this to extend:
<?php
class BaseController extends Zend_Controller_Action
{
public function init()
{
//setup translation object
}
}
?>
You might want to consider to use a dependency injector container, where the translator is an entry that you pass to the objects you need, without manually constructing the object. That way you can easily test and make more high quality (and testable) code
See other question here
How to use dependency injection in Zend Framework?
or this article about plugging ZF 2 DI into ZF1
http://adam.lundrigan.ca/2011/06/using-zenddi-in-zf1/