Accessing session variables PHP

I am trying to access a session variable from a log in form (using the Log In User Server Behavior) and am lost. I simply want to display the users information in the target page. I read in another discussion that I had to bind the variable to the target page, which I did, but that didn't fix it.
Please help.
Here is my loginFormAction code:
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
if (isset($_POST['email'])) {
  $loginUsername=$_POST['email'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "owner.php";
  $MM_redirectLoginFailed = "register.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_petriever, $petriever);
  $LoginRS__query=sprintf("SELECT email, password FROM owners WHERE email=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
  $LoginRS = mysql_query($LoginRS__query, $petriever) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;      
    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    header("Location: " . $MM_redirectLoginSuccess );
  else {
    header("Location: ". $MM_redirectLoginFailed );
?>

Wherever you want to display the logged-in username simply place this on the first line of a .php page to start the session:
<?php session_start(); ?>
Then place this code wherever you want to display the username:
<?php echo $_SESSION['MM_Username']; ?>

Similar Messages

  • Accessing session variables with php

    I am trying to access the  session variable  $_SESSION['MM_Username']  in one of my php pages, but it is somehow showing empty (i.e it does not contain the username that was entered during login).
    I checked and confirmed that my login.php function is properly setting the MM_Username session variable by echoing it  from the login function.
    So why can't I read it from another php file in the same session?  Do I need to do something else before the session variables can
    be properly read from any php file in the same session?  Any help would be appreciated.

    Here is a test code I am using to access the session variable $_SESSION['MM_username']  from  the php page  test.php. But it is not working.
    I get an empty string all the time for $username. Can any one see something wrong with this code?
    <?php require_once('Connections/MyTestingConn.php'); ?>
    <?php session_start(); ?>
    <?php
    $username = "-1";
    if (isset($_SESSION['MM_username'])) {
      $username = $_SESSION['MM_username'];
      echo $username;
    else
       echo "Eror: can not access session variable";
    ?>
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    </head>
    <body>
    </body>
    </html>

  • Access session variable inside AM module

    Is there any way to access session variable inside AM module custom function?
    I created one client interface function in AM and want to access one session variable value inside AM custom function.
    Please suggest.
    Thanks

    Hi
    here i need help to find out right approach,my use case are below
    1.User will login thorough SSO .
    2.Based on used id i need to display user data in table form.
    3.User-id will need to display in right corner of page template ( JSF page with this page template)
    4.user can update/create new table data by pressing update/createinsert button.
    5.Once user press the create new request button,one empty row will popup which have user-id filled automatically (used-id who logged in) and will read only ,so that once user commit the data,DB can update/insert the data with that user-id
    so far i have done point 1,2,4 and 5 like
    1.i created AM preparesession and store the use id.
    2.i have putted whereclause in AM for particular VO and run execute query and able to see particular user data,who logged in.
    4.created servicemethod for createinsert in AM and exposed it ,and set used-is table attribute which i got in AM preparession .Now i am able to see new row with user-id in one column in read only form when
    Is this the right approach ? and now i am not able to do step 3 so how to display same user-id in pagetemplate as this value is available in AM only.

  • Access Session variable inside Transaction

    Hi All,
    Is it possible to access session variables inside Transaction.
    Create session variable.
    Update session variable value
    acccess Session variable.
    (user session)
    Regards,
    Sachin

    Hi Sachin,
    Here is the snapshot:
    1. Take session variable in a local JS variable in HTML page.
    2. Create a transaction with a input type variable in Transaction Properties.
    3. Create a Xacute query and link the above transaction.
    4. Creata a iCommand applet on HTML page and link the query template:
    <PARAM NAME="QueryTemplate" VALUE=".../QueryName">
    Call the function in the below way in HTML:
    Applet.getQueryObject().setParam(1,vaiable)
    So, the session variable will go to TRX (BLT) and perform the operations as per your need..
    You can pass it to other TRX by the same way.
    To fetch the value: Create output type variable in TRX 2. Create Xacute query.
    Get the value with : Applet.getValue(1,1)
    Hope it is useful.
    Best Regards,
    Kedar

  • Access session variable in Java Function in JSP

    Hi Experts !!
    I am developing an application using STRUTS MVC...
    Very sorry if u have problem understanding my question, i ll try to improve... and sorry if i can't post codes here
    Basic question is ....
    I want to open a word document on pageLoad of JSP, the word document is not a single document, there is a form in which there is a "name" and "template" whenever user clicks on a button there is one action attached to it which creates a copy of that template in a different folder.. on the next page OnLoad i want to open that particular document. for that i have created a variable and also have set in the session, just want to access it in the below code.
    I have a formbean in which i have a variable, the scope is session, that variable i have put it in session also. but i want to access that variable in a "java function" in JSP so that "onLoad" page that function should work.
    JSP---
    <script type="text/javascript">
    function openDocument() {
    var w = new ActiveXObject("Word.Application");
    var docText;
    var obj;
    var a;
    if (w != null) {
    w.Visible = true;
    obj = w.Documents.Open(I HAVE TO ACCESS THAT VARIABLE HERE);
    </script>
    FORMBEAN----
    public class CreateSOWFormBean extends ActionForm {
    private String workflowName;
    private String comment;
    private String sowTemplate;
    private String sowFileCreated;
    public String getSowFileCreated() {
    return sowFileCreated;
    public void setSowFileCreated(String sowFileCreated) {
    this.sowFileCreated = sowFileCreated;
    sowFileCreated is the variable that i have accessed in session and that value i have to pass in that function in JSP....
    I am aware of something like
    obj = w.Documents.Open(<%'sowFileCreated'%>);
    but i m not sure how to write....
    Plz help.....

    If you're working under a framework like struts you should definitely be using JSTL tags rather than scriptlet code to access variables within the page. With JSTL code <% codes can be almost entirely avoided.
    To transfer a value from a Servlet to a JSP don't use a session variable, use a request attribute. Session variables should only be used when values have to survive from one transaction to another.
    You can write something like:
    obj = w.Documents.open('<c:out value="${openURL}"/>');in the Javascript portion of your JSP.
    Just beware of potential problems with quotes. The coresponding code in the Servlet would be like:
    request.setAttribute("openURL", openUrl);

  • Accessing session variable in Flex.

    Hi All,
       I'm using RemoteObjects for accessing data and using Spring BlazeDS. How to retrieve 'session' variable values in Flex?
       Main application which is calling different .mxml files. It first invokes login(login.mxml) page with successful user credentials it will display home(home.mxml) page and I have search(search.mxml) pages.
        In case user session timeouts then I'd like to send user request to login page for login.(say user is in search page and where he left for 30 minutes, after then when user clicks anything on search page, it should redisplay login page because session timeout is set to (25 minutes).  How to do this Felx??
       Thanks in advance.
    Regards,
    Sharath.

    More detail solution will be found in the help.
    Look for the following string in your local help (in flex builder): Communicating with the Wrapper.
    You will probably find that you have to do something like the following:
    a) call from flex to the wrapper page javascript function.  (this would be a polling operation, run on a timer event (say) once every minute.
    b) the javascript function you call conducts an Ajax call to determine the current session state.
    Note that the above will likely keep your session state alive.
    Another possibility is to pass in the value of your session timeout via a FlashVars variable.
    Again, in a timer function run every minute, invcrement a local counter.
    When local counter equals session passed in, then you might assume that the session has timed out.
    IHTH.
    Cheers

  • Access Session variable across the hosts

    Hi,
    I am facing an issue. Please find the following scenario.
    I have a login page which is under a secure host. When the login is successfull, the control will be directed to a non secure host, where I have to access a session variable which is set in the LoginPage (which is a secure host). Can I do this?
    Can any one help me in solving this
    regards,
    Jo
    Message was edited by:
    JoshyzJava

    To retreive a varable from another JVM or indeed another host you would need to use Remote Method Invokation (RMI), calling getSecureSessionID() or isSessionOK(sessionID) or whatever from the programme running in the remote JVM.
    The method in general is floored unless the RMI is secure, I have no experience of encrypting this but I'm sure it is possible. You're non-secure-host must have the funcionality to communicate with the secure-host securly, otherwise the secure-host is not secure.
    Bamkin

  • Accessing session variables....urgent

    Hi,
    i have a session variable in my JSF application that stores the userId when a user logs in!!
    i need to access the session variable in a method in the bean.
    can HttpSession and HttpServletRequest objects be accessed in the managed bean
    help me with the code please.....itzz urgent

    see http://forum.java.sun.com/thread.jspa?forumID=427&threadID=545939

  • Accessing session variables

    Hi ,
    I have session variable defined using portals (forms or plsql portlets).
    Can i access these variables using jsp portlets ??
    This is an urgent requirement ..
    Regards,
    Vijaya

    "145822",
    If your application with application id 100, for example, uses a common authentication scheme with application 200 (using the same cookie name in the case of an HTML DB style authentication scheme), then the two applications can run in the same session. A potential benefit of this, even if the two apps are not intricately related, is that the user has only to authenticate once per new session and will have access to both apps. If you set it up this way, then app 100 can access app 200's items in the same session using this function from within app 100:  htmldb.application.fetch_app_item(p_item=>'ITEM_NAME',p_app=>200);Scott

  • Access Session Variable

    Hi All,
    I am using cutom autentication schema called "My Auth Scheme". I want to hide some of the menu options (Tree List) based on the Role Type.
    Could anybody give me any pointer about how to acheive this. And most importantly how to access the session variable (Though I am not even sure about Role Type is going into session or not). Please guide me..
    Regards
    Amit Arora

    Amit,
    You're using a lot of terms that you have not defined:
    I am using cutom autentication schema called "My Auth Scheme". It's an authentication scheme but what is the relevance of it being custom (in what way is it custom and how is that relevant?) and why do we care what you named it?
    I want to hide some of the menu options (Tree List) based on the Role Type. "Role Type" -- What is that?
    And most importantly how to access the session variable ...What do you mean? What session variable?
    Please describe your exact functional requirements, what you are doing now to achieve them and how your actual results differ from the expected results. Also, show us all of the code you are using and demonstrate the problem on apex.oracle.com so we can see exactly what you're trying to do.
    Scott

  • Problem accessing session variables in JSP

              Hi all,
              i am retrieving the value of session variables in a JSP page. the session varibale
              is set at the
              server side. sometimes the jsp is able to retrieve the value and sometimes it
              doesn't.
              Thanks & Regards
              Tarun
              

    if getAttribute return null so there is no such session attribute,anyway
              check your browser if cookies are enabled.
              "Tarun Mangal" <[email protected]> wrote in message
              news:400759e9$[email protected]..
              >
              > Hi all,
              > i am retrieving the value of session variables in a JSP page. the session
              varibale
              > is set at the
              > server side. sometimes the jsp is able to retrieve the value and sometimes
              it
              > doesn't.
              >
              > Thanks & Regards
              > Tarun
              

  • Problem accessing session variable from jsp

    Hi!
    I get an error retrieving a session variable from inside a JSP declaration scriptlet....
    Something like this code in a JSP page:
    <%!
    public void funtion1() {
    String parameter1 = session.getAttribute("parameter1");
    %>I have seen that I have available getContextServer() method and from there I could call getAttribute("parameter1") . But the thing is that the attribute I want to retrieve was stored in session by another Jsp page.
    Any solutions are welcome
    Cheers
    Victor

    Appropriately the above is one the reasons also You cannot use JSP implicit objects from the declaration tag. However we can think of a small workaround by passing the reference like the one discussed below
    <%!public void funtion1(HttpSession session) {
    String parameter1 = session.getAttribute("parameter1").toString();
    }%>
    <% funtion1(session);%>Hope that helps :)
    REGARDS,
    RaHuL

  • Problems accessing session variables from a pop-up

    I am working on a webapp and I when I click on a certain button I need to have a pop-up appear that will either prompt a user to enter log-in info if they haven't already, otherwise the pop-up just needs to display a report. Users can navigate around on the main page and select various reports so multiple pop-ups could possibly be opened. When a user logs in I save the login info in session variables, but for some reason whenever I click to have another pop-up open it always says that my session variables are null. Is there a way to have more global session variables so that multiple windows (the pop-ups) can touch session variables? Would there be a way to pass the login info in session variables back to the main page and then pass those session variables back out to each pop-up that opens? Thanks so much.

    Have you got session cookies enabled?
    The most common way to keep track of the session id is via a session cookie.
    This cookie gets shared by all windows of the same browser session.
    You are opening these pop-up windows with window.open?
    Print out the session id <%= session.getId() %> on each page to see if it is the same.
    Cheers,
    evnafets

  • How to use session variable in JSP function  & How to use both JSP  Servlet

    Hi,
    I am new to JSP and servlets
    Still I am devloping a website in JSP. I am not mixing JSP with servlets, but I do create Java files for bean, logic and database works.
    I try to keep the hard coding part out of JSP.
    I dont how to use both JSP and Servlets in combination.
    Hence If needed I write some functions in JSP.
    but it gives me error
    +<%! public void abc()+
    +{+
    int intUserId = Integer.valueOf((Integer) session.getAttribute("MySession_UserID"));
    +}+
    +%>+
    Saying cannot find symbol session
    1) So can u please tell how can I access session variables within JSP function
    2) And also give me some links/tutorials about useing both JSP and Servlets in combination.
    Thanks
    Venkat

    The application architecture when you use Servlets and JSP in a standard MVC pattern is explained here (under the heading "Integrating Servlets and JSP Pages") and here ...

  • Help with session variable please - CS5.5 PHP

    Hi all,
    I am needing a little assistance with using a session variable and hope that someone may point me in the right direction.
    I have created a PHP page that uses the Dreamweaver 'User Authentication' feature, and the basics of this works fine, directing a user to the correct pages depending on whether they are or are not a valid user. I would like however to personalise the 'valid user' page with the persons Username as entered in the User Authentication table....a seemingly simple task using a session variable, but one that I just don't seem to be able to get working!
    The code generated for the UA on page 1 is as follows:
    <?php
    // *** Validate request to login to this site.
    if (!isset($_SESSION)) {
      session_start();
    $loginFormAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
      $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    if (isset($_POST['txtfirst_name'])) {
      $loginUsername=$_POST['txtfirst_name'];
      $password=$_POST['txtsurname'];
      $MM_fldUserAuthorization = "";
      $MM_redirectLoginSuccess = "member_update.php";
      $MM_redirectLoginFailed = "login.php";
      $MM_redirecttoReferrer = false;
      mysql_select_db($database_panto, $panto);
      $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND surname=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
      $LoginRS = mysql_query($LoginRS__query, $panto) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
        //declare two session variables and assign them
       $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;         
        if (isset($_SESSION['PrevUrl']) && false) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   
        header("Location: " . $MM_redirectLoginSuccess );
      else {
        header("Location: ". $MM_redirectLoginFailed );
    ?>
    Firstly, the text highlighted in red above appears to be setting the session variable that I require. Is This correct?
    If so, what is the code that I need to place in page 2 to use that session variable? or
    Do I need to do something else on page 1 to properly assign the session variable?
    Would really appreciate your expertise
    Mark

    It looks as though you've set the columns incorrectly in the User Authentication server behavior. This is the SQL query that checks the user's credentials:
      $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND surname=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
    You're looking for firstname and surname, whereas you should be looking for the user's login name and password.
    $_SESSION['MM_Username'] is a session variable that stores the user's login name. To use it in a subsequent page, all that's needed is for the page to begin with session_start(). You can then echo the value to display it.
    If you want to display the person's real name, you need to create a recordset in the second page using $_SESSION['MM_Username'] to find the firstname and surname. Alternatively, you can edit the existing code like this (I've copied only part of it):
      $LoginRS__query=sprintf("SELECT firstname, surname FROM web_access WHERE firstname=%s AND password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
      $LoginRS = mysql_query($LoginRS__query, $panto) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        $row = mysql_fetch_assoc($LoginRS);
         $_SESSION['full_name'] = $row['firstname'] . ' ' . $row['surname'];
        if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
        //declare two session variables and assign them
       $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;
    You can then use $_SESSION['full_name'] in a subsequent page that begins with session_start().

Maybe you are looking for