how to passs and use variables in different scripts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuragpj
    New Member
    • Jan 2007
    • 32

    how to passs and use variables in different scripts

    i had stored a variable in a php file. now I want to use this variable in some other script how can ii do this?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You can either store it in the $_SESSION array or include the script in which you want to use the variable.
    When you want to use variable $var in script newscript.php, you can include it:[php]]<?php
    ... some coding ....
    $var = 'value';
    include('newscr ipt.php');
    ... some more coding ....[/php]
    See the following threads




    about passing variables between scripts using the $_SESSION array.

    Ronald :cool:

    Comment

    • anuragpj
      New Member
      • Jan 2007
      • 32

      #3
      Originally posted by ronverdonk
      You can either store it in the $_SESSION array or include the script in which you want to use the variable.
      When you want to use variable $var in script newscript.php, you can include it:[php]]<?php
      ... some coding ....
      $var = 'value';
      include('newscr ipt.php');
      ... some more coding ....[/php]
      See the following threads




      about passing variables between scripts using the $_SESSION array.

      Ronald :cool:
      this is my login.php

      [PHP]<?
      session_start() ;
      $user=$_post['username'];
      $_session['username']=$user;
      ?>
      <title>Electric al Engineering Dept IITK</title>
      <body bgcolor="#99CCF F">
      <form name="form1" method="post" action="../myphp/validate1.php">
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <table width="38%" border="0">
      <tr>
      <th scope="row"><di v align="left">
      <label>User Name</label>
      &nbsp;</div></th>
      <td><input name="username" type="text" size="35" maxlength="35"> </td>
      </tr>
      <tr>
      <th height="29" scope="row"><di v align="left">Pa ssword</div></th>
      <td><input name="password" type="password" size="35" maxlength="35"> </td
      >
      </tr>
      </table>
      &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;
      <p>&nbsp;</p>
      <table width="2%" border="0" align="center">
      <tr>
      <th scope="row"><in put type="submit" name="Submit" value="Login"></th>
      <td><input type="submit" name="Submit2" value="Cancel"> </td>
      </tr>
      </table>
      <p>&nbsp;</p>

      <label><u><stro ng></strong></u></label>
      </p>
      </div>
      </form>
      </body>[/PHP]

      this is my fetch.php
      [PHP]
      <?php
      session_start() ;
      $username1=$_se ssion['username'];
      $connection = pg_connect("hos t=localhost dbname=personal user=postgres") ;
      if (!$connection) {
      print("Connecti on Failed.");
      exit;
      }
      $myresult=pg_ex ec($connection, "select * from personal where username='$user name1'");


      for ($lt = 0; $lt < pg_numrows($myr esult); $lt++) {
      $department = pg_result($myre sult, $lt, 2);
      $telephonework = pg_result($myre sult, $lt, 3);
      $telephoneres = pg_result($myre sult, $lt, 4);
      $fax = pg_result($myre sult, $lt, 5);
      $email = pg_result($myre sult, $lt, 6);
      $address = pg_result($myre sult, $lt, 7);
      $areaofinterest = pg_result($myre sult, $lt, 8);
      $username= pg_result($myre sult, $lt, 9);
      $image= pg_result($myre sult, $lt, 10);
      }
      ?>

      <body bgcolor="#FFCC9 9">
      <form name="form1" method="post" action="pupdate .php">
      <tr>
      <th width="148" scope="row"><di v align="left">
      <label>IMAGE</label>
      </div></th>
      <td width="361">
      <div align="center">
      </div>
      </td>
      <input type="submit" name="Submit" value="Upload">
      </div>
      </td>
      </tr>
      <tr>
      <th scope="row"><di v align="left">
      <label>USERNAME </label>
      </div></th>
      <td>
      </div>
      </td>
      <td></td>
      </tr>
      <tr>
      <th scope="row"><di v align="left">
      <label>NAME</label>
      </div></th>
      </div>
      </td>
      <td></td>
      </tr>
      <tr>
      <th scope="row">
      </div></th><td><div align="left">
      </div></td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      </div></th>
      <td>
      <div align="left">
      </div>
      </td>
      <td></td>
      <th scope="row"><di v align="left"><l abel>TELEPHONE( Work)</label>
      </label>
      </div></th><td><div align="left">
      </div></td>
      <td>&nbsp;</td>
      </tr>
      </label>
      </div></th><td><div align="left">
      </div></td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <th scope="row"><di v align="left">
      <td><div align="left">
      </div></td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <th scope="row"><di v align="left">
      <label>EMAIL</label>
      </div></th>
      </div></td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <th scope="row"><di v align="left">
      <label>ADDRES S</label>
      </div></th>
      <td>
      <div align="left">
      </div>
      </td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <th height="150" scope="row">
      <div align="left">
      <label>AREA OF INTEREST</label>
      &nbsp;</div></th>
      <td><div align="left">
      <textarea name="areaofint erest" cols="46" rows="8"><? echo "$areaofint eres
      t";?></textarea>
      </div>
      </td>
      <td>&nbsp;</td>
      </tr>
      </table>
      <div align="center">
      <input type="submit" name="submit" value="Update">
      <input type="reset" name="reset" value="Clear It">
      </label>
      </label>
      </div>
      </form>
      </body>
      </html>[/PHP]

      it is not working. can u tell me where i m going wrong?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Please specify what is not working? I see scripts login.php and fetch.php. Who is calling who? And where is script validate1.php? Because that is the script the login,php submit s to and the script that received the userid from the form in its POST array.
        Code in login.php
        Code:
        session_start();
        $username1=$_session['username'];
        will not work because you have not assigned the username to the $_SESSION array as far as I can see from any script.

        So the question is: what script submits to what script? And where is validate1.php?

        Ronald :cool:

        Comment

        • anuragpj
          New Member
          • Jan 2007
          • 32

          #5
          Originally posted by ronverdonk
          Please specify what is not working? I see scripts login.php and fetch.php. Who is calling who? And where is script validate1.php? Because that is the script the login,php submit s to and the script that received the userid from the form in its POST array.
          Code in login.php
          Code:
          session_start();
          $username1=$_session['username'];
          will not work because you have not assigned the username to the $_SESSION array as far as I can see from any script.

          So the question is: what script submits to what script? And where is validate1.php?

          Ronald :cool:
          this is my validate.php

          [PHP]<?php
          $passwords = array("ajohari" =>"anurag",
          "knaveen" =>"naveen",
          "anu" =>"anu");

          if ($password == $passwords[$username]){
          //setcookie("user name", $username, time()+1200);
          header("locatio n:/myphp/profile.php");
          exit();
          }else{
          //setcookie("user name", "", time()-3600);
          echo "<H2>Invali d user name or password: access denied.</H2>";
          }
          ?>[/PHP]

          how can I store the value of username in a session array?

          login script is calling validate.php and after that fetch.php is called.
          Last edited by anuragpj; Jan 25 '07, 05:22 AM. Reason: something is wrong

          Comment

          • anuragpj
            New Member
            • Jan 2007
            • 32

            #6
            Originally posted by anuragpj
            this is my validate.php

            [PHP]<?php
            $passwords = array("ajohari" =>"anurag",
            "knaveen" =>"naveen",
            "anu" =>"anu");

            if ($password == $passwords[$username]){
            //setcookie("user name", $username, time()+1200);
            header("locatio n:/myphp/profile.php");
            exit();
            }else{
            //setcookie("user name", "", time()-3600);
            echo "<H2>Invali d user name or password: access denied.</H2>";
            }
            ?>[/PHP]

            how can I store the value of username in a session array?

            login script is calling validate.php and after that fetch.php is called.
            reply please

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              It is exactly as I told you:
              [php]<?php
              session_start() ;
              $passwords = array("ajohari" =>"anurag",
              "knaveen" =>"naveen",
              "anu" =>"anu");

              if ($password == $passwords[$username]){
              $_SESSION['username'] = $username;
              //setcookie("user name", $username, time()+1200);
              header("locatio n:/myphp/profile.php");
              exit();
              }else{
              //setcookie("user name", "", time()-3600);
              echo "<H2>Invali d user name or password: access denied.</H2>";
              }
              ?>[/php]
              Ronald :cool:

              Comment

              Working...