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?
how to passs and use variables in different scripts
Collapse
X
-
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.phpOriginally posted by ronverdonkYou 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:
[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> </p>
<p> </p>
<p> </p>
<table width="38%" border="0">
<tr>
<th scope="row"><di v align="left">
<label>User Name</label>
</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>
&nb sp; &nb sp;
<p> </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> </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> </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> </td>
</tr>
</label>
</div></th><td><div align="left">
</div></td>
<td> </td>
</tr>
<tr>
<th scope="row"><di v align="left">
<td><div align="left">
</div></td>
<td> </td>
</tr>
<tr>
<th scope="row"><di v align="left">
<label>EMAIL</label>
</div></th>
</div></td>
<td> </td>
</tr>
<tr>
<th scope="row"><di v align="left">
<label>ADDRES S</label>
</div></th>
<td>
<div align="left">
</div>
</td>
<td> </td>
</tr>
<tr>
<th height="150" scope="row">
<div align="left">
<label>AREA OF INTEREST</label>
</div></th>
<td><div align="left">
<textarea name="areaofint erest" cols="46" rows="8"><? echo "$areaofint eres
t";?></textarea>
</div>
</td>
<td> </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
-
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.phpwill not work because you have not assigned the username to the $_SESSION array as far as I can see from any script.Code:session_start(); $username1=$_session['username'];
So the question is: what script submits to what script? And where is validate1.php?
Ronald :cool:Comment
-
this is my validate.phpOriginally posted by ronverdonkPlease 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.phpwill not work because you have not assigned the username to the $_SESSION array as far as I can see from any script.Code:session_start(); $username1=$_session['username'];
So the question is: what script submits to what script? And where is validate1.php?
Ronald :cool:
[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.Comment
-
reply pleaseOriginally posted by anuragpjthis 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.Comment
-
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
Comment