By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,822 Members | 1,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,822 IT Pros & Developers. It's quick & easy.

how to passs and use variables in different scripts

P: 32
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?
Jan 23 '07 #1
Share this Question
Share on Google+
6 Replies


ronverdonk
Expert 2.5K+
P: 4,258
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('newscript.php');
... some more coding ....[/php]
See the following threads

http://www.thescripts.com/forum/thread591800.html
http://www.thescripts.com/forum/thread591691.html

about passing variables between scripts using the $_SESSION array.

Ronald :cool:
Jan 23 '07 #2

P: 32
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('newscript.php');
... some more coding ....[/php]
See the following threads

http://www.thescripts.com/forum/thread591800.html
http://www.thescripts.com/forum/thread591691.html

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>Electrical Engineering Dept IITK</title>
<body bgcolor="#99CCFF">
<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"><div 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"><div align="left">Password</div></th>
<td><input name="password" type="password" size="35" maxlength="35"></td
>
</tr>
</table>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;
<p>&nbsp;</p>
<table width="2%" border="0" align="center">
<tr>
<th scope="row"><input type="submit" name="Submit" value="Login"></th>
<td><input type="submit" name="Submit2" value="Cancel"></td>
</tr>
</table>
<p>&nbsp;</p>

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

this is my fetch.php
[PHP]
<?php
session_start();
$username1=$_session['username'];
$connection = pg_connect("host=localhost dbname=personal user=postgres");
if (!$connection) {
print("Connection Failed.");
exit;
}
$myresult=pg_exec($connection, "select * from personal where username='$username1'");


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

<body bgcolor="#FFCC99">
<form name="form1" method="post" action="pupdate.php">
<tr>
<th width="148" scope="row"><div 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"><div align="left">
<label>USERNAME</label>
</div></th>
<td>
</div>
</td>
<td></td>
</tr>
<tr>
<th scope="row"><div 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"><div align="left"><label>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"><div align="left">
<td><div align="left">
</div></td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><div align="left">
<label>EMAIL</label>
</div></th>
</div></td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><div align="left">
<label>ADDRESS</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="areaofinterest" cols="46" rows="8"><? echo "$areaofinteres
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?
Jan 24 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
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
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. $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:
Jan 24 '07 #4

P: 32
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
Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. $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("username", $username, time()+1200);
header("location:/myphp/profile.php");
exit();
}else{
//setcookie("username", "", time()-3600);
echo "<H2>Invalid 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.
Jan 25 '07 #5

P: 32
this is my validate.php

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

if ($password == $passwords[$username]){
//setcookie("username", $username, time()+1200);
header("location:/myphp/profile.php");
exit();
}else{
//setcookie("username", "", time()-3600);
echo "<H2>Invalid 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
Jan 29 '07 #6

ronverdonk
Expert 2.5K+
P: 4,258
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("username", $username, time()+1200);
header("location:/myphp/profile.php");
exit();
}else{
//setcookie("username", "", time()-3600);
echo "<H2>Invalid user name or password: access denied.</H2>";
}
?>[/php]
Ronald :cool:
Jan 29 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.