Jump to content

Archived

This topic is now archived and is closed to further replies.

jmahdi

assigning a session to a variable , twice!?

Recommended Posts

hi there,

i'm trying to put a message in the footer of a page which welcomes a person who is logged in with his/her name, using sessions of course; when i place this:

$username = $_SESSION['valid_user'];

 

in the footer before the echo:

echo "You are logged in as $username";

 

but the session is also needed before the footer to use the username for other things , such as -checking his credit- so if place in footer the footer shows name in browser but checking credit would not happen as the assignment is at the buttom.

 

IF i place the assignment above at the top of the file:

 

everything works for the user and checking credit ..etc...but the footer is not there...

 

cud not put this any clearer..sorry...hope if someone cud help...thanks

Share this post


Link to post
Share on other sites

Set the session at the top of the webpage before the <html> tag.

 

 

For example

 

 

<?
//Start Sessions
session_start();


//Set Session
$_SESSION['valid_user'] = "john.doe";


//Assign Session to Variable
$username = $_SESSION['valid_user'];
?>
<html>


<head>
</head>


<body>


<!-- RUN CHECKS HERE, for example -->
<? mysql_query("SELECT * FROM credit WHERE username='$username'");  ?>
<!-- END -->


<!-- FOOTER HERE -->
<? echo "You are logged in as $username"; ?>


</body>


</html>

 

 

That is how I do it most of the time. But set the sessions in an external document and call that into the current HTML page above the HTML tag to stop you having to change each individual file where a change is needed.

 

 

Like this :

 

 



<? include_once "./includes/scripts/sessions.inc.php"; ?> 
<html>
<head></head>
<body></body>
</html>


 

 

Did this help?

 

 

George.

Share this post


Link to post
Share on other sites

Set the session at the top of the webpage before the <html> tag.

 

 

For example

 

 

<?
//Start Sessions
session_start();


//Set Session
$_SESSION['valid_user'] = "john.doe";


//Assign Session to Variable
$username = $_SESSION['valid_user'];
?>
<html>


<head>
</head>


<body>


<!-- RUN CHECKS HERE, for example -->
<? mysql_query("SELECT * FROM credit WHERE username='$username'");  ?>
<!-- END -->


<!-- FOOTER HERE -->
<? echo "You are logged in as $username"; ?>


</body>


</html>

 

 

That is how I do it most of the time. But set the sessions in an external document and call that into the current HTML page above the HTML tag to stop you having to change each individual file where a change is needed.

 

 

Like this :

 

 



<? include_once "./includes/scripts/sessions.inc.php"; ?> 
<html>
<head></head>
<body></body>
</html>


 

 

Did this help?

 

 

George.

exept get into the habit of using long tags...<?php

Share this post


Link to post
Share on other sites

Apologies.

 

 

Always use long tags..

 

 

My team of developers work on our servers with short tags enabled.

 

 

George.

Share this post


Link to post
Share on other sites

Apologies.

 

 

Always use long tags..

 

 

My team of developers work on our servers with short tags enabled.

 

 

George.

not a problem at all...most people dont have them enabled, so just to avoid further errors...anyway jmahdi what do you receive when you try to echo $_SESSION['valid_user']; in your footer?

Share this post


Link to post
Share on other sites

Set the session at the top of the webpage before the <html> tag.

 

 

For example

 

 

<?
//Start Sessions
session_start();


//Set Session
$_SESSION['valid_user'] = "john.doe";


//Assign Session to Variable
$username = $_SESSION['valid_user'];
?>
<html>


<head>
</head>


<body>


<!-- RUN CHECKS HERE, for example -->
<? mysql_query("SELECT * FROM credit WHERE username='$username'");  ?>
<!-- END -->


<!-- FOOTER HERE -->
<? echo "You are logged in as $username"; ?>


</body>


</html>

 

 

That is how I do it most of the time. But set the sessions in an external document and call that into the current HTML page above the HTML tag to stop you having to change each individual file where a change is needed.

 

 

Like this :

 

 



<? include_once "./includes/scripts/sessions.inc.php"; ?> 
<html>
<head></head>
<body></body>
</html>


 

 

Did this help?

 

 

George.

 

i use a similar methodology..but not as neat to say the truth ;D the session is already assigned to the user who logs n-in a seperate file in this way:

 

$_SESSION["valid_user"] = $username;

 

then on each other page that follows the login page- one of them is the one in question- i place the following:

 

session_start(); 

if (!isset($_SESSION['valid_user'])) {
header('Location: login.htm');
}

    
$username = $_SESSION['valid_user'];

 

before the html tag...but it didnt work for the footer, on the same page the footer looks like this:

 

<div id="Footer" style="height:5%; background:blue;"><?php
  
    echo "You are logged in as $username"; ?>
        </div>

 

Can i assign the session to $username  again , itried this but it didn't work?!  thanks

Share this post


Link to post
Share on other sites

How are you including the footer code into/on your page? Posting enough of your code that demonstrates/reproduces the problem would help.

 

Also, your code that redirects if the visitor is not logged in, needs an exit; statement after the header(); statement to prevent the remainder of the code on your 'protected' pages from being executed.

Share this post


Link to post
Share on other sites

How are you including the footer code into/on your page? Posting enough of your code that demonstrates/reproduces the problem would help.

 

Also, your code that redirects if the visitor is not logged in, needs an exit; statement after the header(); statement to prevent the remainder of the code on your 'protected' pages from being executed.

 

thanks....here is all the code:

<?php
// start the session.
session_start(); 

if (!isset($_SESSION['valid_user'])) {
header('Location: login.htm');
exit();
}
$tid = $_SESSION['trackid'];

    
$username = $_SESSION['valid_user'];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>        
        <title>Download Page</title>

<link rel="stylesheet" type="text/css" href="mycss.css" />
    </head>
    <body>
<div id="Main" style="width:600px; height:800px; background: green; margin: auto; padding: 5px;">
            <div id="Header" style="height:10%; background:red;">
	<div  id="Menu" style="margin-left:0; padding-left:0;" >
  <ul>
   <li>
<a href="index.htm">HOME</a> 
   </li>
    <li>
<a href="update.htm">MY ACCOUNT</a> 
</li>
   <li>
<a href="regis.htm">REGISTER</a>
</li>
   <li>
<a href="content.htm">BROWSE MUSIC</a>
</li>
   <li>
<a href="contact.htm">CONTACT</a>
</li>
   <li>
<a href="logout.php">SIGN OUT</a>
</li>
</ul>
</div> </div>
            <div id="Content" style="height:85%; background:yellow;"><?php
include "dbconfig.php";


$credit = mysql_query("SELECT creditin FROM  user WHERE username = '$username' ");
        
     $row = mysql_fetch_assoc($credit);
        
        $credit = $row['creditin'];

//gets the file
$file = $_GET['action'];

$tid = $_SESSION['trackid'];

$price= $_SESSION['price'];

//get file by id
$price = mysql_query("SELECT * FROM tracks WHERE trackID='$file'")
or die("Insert Error: ".mysql_error());
	/******************************************************/
$row = mysql_fetch_assoc($price);
        $price = $row['price'];

/******************************************************/
$seller = $row['userID'];
$buyer = $username;

 $x = mysql_query("SELECT * FROM user WHERE 	username='$username'");
  $xx = mysql_fetch_assoc($x);
  $finalmoney=$xx['creditin']-$price;
  if($finalmoney >=0){
  
 $y = mysql_query("SELECT * FROM user WHERE 	userID='$seller'");
  $yy = mysql_fetch_assoc($y);
  $yourmoney=$yy['creditin']+$price;
 // echo "-->".$yourmoney;


mysql_query("UPDATE user SET creditin = '$finalmoney' WHERE username='$username'");
mysql_query("UPDATE user SET creditin = '$yourmoney' WHERE userID='$seller'");

		     
echo'
<script type="text/javascript">

window.location = "download.php?action='.$file.'";

</script>	
';
}else{
echo "Sorry, no enough CASH!";
}

?>
</div>
            <div id="Footer" style="height:5%; background:blue;"><?php
  
    echo "You are logged in as $username"; ?>
        </div>
</body>

Share this post


Link to post
Share on other sites

Any chance that the dbconfig.php code is also setting the $username variable?

 

Are you getting just this part of the footer - "You are logged in as"?

Share this post


Link to post
Share on other sites

Any chance that the dbconfig.php code is also setting the $username variable?

 

Are you getting just this part of the footer - "You are logged in as"?

 

no..the dbconfig file got no reference at all to $username , only the database connection stuff...

 

the only time i get the footer in the browser at all is when i do like this- ans it gives me the full welcoming msg: You are logged in as jmahdi for example:

 

 <div id="Footer" style="height:5%; background:blue;"><?php
[color=red]  $username = $_SESSION['valid_user'];[/color]
    echo "You are logged in as $username"; ?>
        </div>

placing the assignment in the footer and removing it from where is was -at te very top...but sure i get nothing working in the page that needs the $username to do other functions...

 

 

Share this post


Link to post
Share on other sites

Any chance that the dbconfig.php code is also setting the $username variable?

 

Are you getting just this part of the footer - "You are logged in as"?

 

I don't know if thats obvious but the file is a .php file....does this make a difference...thanks

Share this post


Link to post
Share on other sites

I think he's wondering if the variable is overwritten somewhere in your code. Why don't you try echoing the session itself in the footer instead of assigning it to a variable and then echoing it?

Share this post


Link to post
Share on other sites

What does a 'view source' in your browser show for the page when it doesn't work?

 

Either your page is redirecting to the download.php?action= page or something in your php code is stopping execution if you don't even get the footer to display in the browser.

Share this post


Link to post
Share on other sites

I think he's wondering if the variable is overwritten somewhere in your code. Why don't you try echoing the session itself in the footer instead of assigning it to a variable and then echoing it?

 

i think i know why the footer does not show up at all: when i rearrange the code given above: (changes in red)

 

mysql_query("UPDATE user SET creditin = '$finalmoney' WHERE username='$username'");
mysql_query("UPDATE user SET creditin = '$yourmoney' WHERE userID='$seller'");

		     
[color=red]?>
</div>
            <div id="Footer" style="height:5%; background:blue;"><?php
  
    echo "You are logged in as $username"; ?>
        </div>
<?php[/color]


echo'
<script type="text/javascript">

window.location = "download.php?action='.$file.'";

</script>	
';
}else{
echo "Sorry, no enough CASH!";
}

?>

</body>

the footer shows up because the the person GOt enough cash, but before it used to show up the footer only after the "Sorry, no enough Cash" echo, i don't know how to show the footer in either cases...

Share this post


Link to post
Share on other sites

What does a 'view source' in your browser show for the page when it doesn't work?

 

Either your page is redirecting to the download.php?action= page or something in your php code is stopping execution if you don't even get the footer to display in the browser.

 

the footer didn't show up because the footer comes after this condition:

 

 if($finalmoney >=0){
  
 $y = mysql_query("SELECT * FROM user WHERE 	userID='$seller'");
  $yy = mysql_fetch_assoc($y);
  $yourmoney=$yy['creditin']+$price;
 // echo "-->".$yourmoney;


mysql_query("UPDATE user SET creditin = '$finalmoney' WHERE username='$username'");
mysql_query("UPDATE user SET creditin = '$yourmoney' WHERE userID='$seller'");

		     
echo'
<script type="text/javascript">

window.location = "download.php?action='.$file.'";

</script>	
';
}else{
echo "Sorry, no enough CASH!";
}

when the user got enough cash and downloads the file ...the footer does not show up ...it only shows up if  ELSE...how to rectify this and place my Footer div in a place where it will show up in both cases..?  thanks

Share this post


Link to post
Share on other sites

anyone knows of a wa to skip the php and jump to the html at the bottom of the code?! I think tis should be the solution for the problem....hopefully

Share this post


Link to post
Share on other sites

ok i had solved...i just used the goto operater to direct the flow to the footer....you can consider this as solved ,, thanks all for your help....

Share this post


Link to post
Share on other sites

ok i had solved...i just used the goto operater to direct the flow to the footer....you can consider this as solved ,, thanks all for your help....

excellent. please mark as solved on the bottom left of this thread.. thanks

Share this post


Link to post
Share on other sites

ok i had solved...i just used the goto operater to direct the flow to the footer....you can consider this as solved ,, thanks all for your help....

excellent. please mark as solved on the bottom left of this thread.. thanks

 

thanx fugix..thanks for the help

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.