passing variable's value from php to others

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Hello all. I've a problem in php.

On a php page, I get the user's informations (user, password, etc...). On another php page, the user can change his password or his profile, but I don't want have to ask him again his user name.

I've tried with session_start(); on the top of the first page, and then $_SESSION['userid']=user_value; but when I retrieve the $_SESSION value in the next php page, there is nothing !

How do I do that ?

1st PHP:

<?php
session_start();
?>
...
<link rel=stylesheet href="the.css" type="text/css">
</head>
<body>
<?php
$con = mysql_connect("localhost", "ident_name", "ident_pw");
$db = "db_ayantdroit";
if (!$con) {die('Connection impossible : ' . mysql_error());}
mysql_select_db($db, $con);
$ut = $_POST['utilisateur'];
$pa = $_POST['upass'];
$sql = "SELECT * FROM ayant_droit WHERE utilisateur = '" . $ut . "'";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) { // l'utilisateur existe dans la base
while($row = mysql_fetch_array($result)) {
if ($row['upass'] == $pa) { // password valide, on charge le menu correspondant au niveau de service
$_SESSION['util']=$ut;
...


2nd PHP :

<html><head><title>Gestion du mot de passe</title>
<link rel=stylesheet href="the.css" type="text/css">
</head>
<body>

<div class="flot"><center>
<form action="thePass.php" method="post">
Mot de Passe actuel :<br /><input type="password" name="upass" /><br />
Nouveau Mot de Passe :<br /><input type="password" name="nupass1" /><br />
V&eacute;rification du Nouveau Mot de Passe :<br /><input type="password" name="nupass2" /><br />
<input type="submit" name="submit" />
</form></center>
</div>
<?php
$ut=$_SESSION['util'];
echo 'début du php de gesPass.php<br />';
echo "nom d'utilisateur = /".$_POST['utilisateur']."/<br />";
echo 'suite du php de gesPass.php';
...


and this is the result of the echoes :

début du php de gesPass.php
nom d'utilisateur = //
suite du php de gesPass.php

Thanks for your help.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
Your problem is around here :
PHP:
$ut=$_SESSION['util'];
echo 'début du php de gesPass.php<br />';
echo "nom d'utilisateur = /".$_POST['utilisateur']."/<br />";
echo 'suite du php de gesPass.php';
...

It should be more like this :
PHP:
$ut=@$_SESSION['util'];
echo 'début du php de gesPass.php<br />';
echo "nom d'utilisateur = /".$_SESSION['util']."/<br />";
echo 'suite du php de gesPass.php';
...

better yet do this :
PHP:
if($ut=$_SESSION['util']) {
echo 'début du php de gesPass.php<br />';
echo "nom d'utilisateur = /".$ut."/<br />";
echo 'suite du php de gesPass.php';
} else {
... // maybe return a error code, not sure what error checking you got going ?
}
...
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
@fomalhaut: Use the [ PHP] [ /PHP], [ CODE] [ /CODE] or [ HTML] [ /HTML] (without the space) when you post code on the forums. It's easier to read, understand and looks better.

Make sure that you added session_start(); to the beginning of both files (very first line)
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
I'm not really following how the 2 pages are connected...

you're trying to call $_POST['utilisateur'], but you're not showing how.


if you could, try posting the whole script and not just snipets, cause it could be something that you wouldn't even think it was ;)


ps, remember [ php ] and [ /php ]
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
For those using [ php ] ... [ /php ], try [noparse][noparse]
PHP:
 ...
[/noparse][/noparse] ;)
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
For those using [ php ] ... [ /php ], try [noparse][noparse]
PHP:
 ...
[/noparse][/noparse] ;)
I didn't know about the [noparse] tag, but it seems that the tag is working so well that even the noparse is not parse! :lol: :biggrin:

On the other side, the full page code will be more helpful in determining errors.
 

a94060

New Member
Messages
16
Reaction score
1
Points
0
Make sure both files are in the same directoy. I ran into this problem. If its a syntax error, sorry
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Thanks for the link! Can I close threads?

formalhaut, make sure that each page starts with:
PHP:
<?php
session_start();
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
I think you can because he replied to me when he gave +rep :)
 

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
what do you mean?

Hello

Yes, Xav0989, I gave an answer. I though I've probably made a bad operation while answering !

Indeed, I thought the
PHP:
<?php session_start(); ?>[ /php]
was needed only on the first page !!!:biggrin:

After having put it on each page, it works very well ! 

Thanks again.

You can close this thread if you want.
 
Last edited:
Top