12 Replies - 1542 Views - Last Post: 25 April 2012 - 07:44 AM Rate Topic: -----

#1 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 06:13 AM

I have problem with setting $UserId a super global variable from the login.php to be used in the neworder.php, using $_SESSION . but wont be shown in the neworder.php while i can echo it in login.php

Note, uEmail and uId are COOKIEs from validate.php the echo works here
Login.php
<?php
if (isset($_COOKIE["uEmail"])){
            $UserId= $_COOKIE["uId"];
            $_SESSION['UserId']=$UserId;

            echo "Welcome " . $_COOKIE["uEmail"] . $_SESSION['UserId']. "!<br />";
}else{
            echo "Please Login!<br />";}
      ?>




Neworder.php
I can't echo the $_SESSION['UserId' here:

 <form action="" method="post" name="fromOrderlist">
  <b></b>
  <p>
    <label for="userId2">User ID:</label><? echo $_SESSION['UserId'];?>
    <label for="customer_name2"><br>

...


please help

This post has been edited by Dormilich: 24 April 2012 - 07:26 AM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: problem using $_SESSION to create a super global variable

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 07:11 AM

Sessions are stored on the server, so when you contact the server you need to tell it to use that session. That's why you need to start a session every time you reload the page.
Was This Post Helpful? 0
  • +
  • -

#3 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 07:21 AM

View PostCTphpnwb, on 24 April 2012 - 07:11 AM, said:

Sessions are stored on the server, so when you contact the server you need to tell it to use that session. That's why you need to start a session every time you reload the page.

I did try that too, i have put
<? session_start(); ?>


on top of the page
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 07:25 AM

This is just a guess because I can't see your code, but you probably are confusing server side code (PHP) with client side code (HTML). First the server side is processed and then later on the client side code is processed on a different machine.

Mixing the two languages in one file doesn't change the order in which they're processed.
Was This Post Helpful? 0
  • +
  • -

#5 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 06:10 PM

Here is the code inside the HTML
<p class="login.four>
<?php
2
if (isset($_COOKIE["uEmail"])){
3
            $UserId= $_COOKIE["uId"];
4
            $_SESSION['UserId']=$UserId;
5
 
6
            echo "Welcome " . $_COOKIE["uEmail"] . $_SESSION['UserId']. "!<br />";
7
}else{
8
            echo "Please Login!<br />";}
9
      ?>





Neworder.php
I can't echo the $_SESSION['UserId' here:

view sourceprint?
1


<form action="" method="post" name="fromOrderlist">
2
<b></b>
3
<p>
4
<label for="userId2">User ID:</label><? echo $_SESSION['UserId'];?>
5
<label for="customer_name2"><br>
6

7
...


Let me know which part of the code would you like to see but you can't i'll post it :) tnx
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 07:16 PM

You need to start a session before sending any HTML, so it would need to be before line 1 of your code above.

Here's what this says to the interpreter:
<p class="login.four>  *** NOT PHP, so just pass this to the browser ***
<?php *** PHP code follows, so execute it ***


Since you've passed stuff off to the browser it is now too late to start a session, which you would do with:
session_start();


Was This Post Helpful? 0
  • +
  • -

#7 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 24 April 2012 - 08:31 PM

This is the full code, it doesn't work :(




<?php
session_start();
if (isset($_COOKIE["uEmail"])){
    $UserId= $_COOKIE["uId"];
	$_SESSION['UserId']=$UserId;
}
?>
      
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My First Site</title>
<link rel="stylesheet" type="text/css" href="FirstSite.css"></link>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="drop.js"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<!--main container-->
<div class="main_container">
<div class="title">
<div class="drpmnu1">
	 <table class="drpmnu1">
		<td class="drpmnu1"><div class="one"><p class="menu1">Taobao Order</p></div></td>
		<td class="drpmnu1"><div class="two"><p class="menu1">Payments</p></div></td>
		<td lass="drpmnu1"><div class="three"><p class="menu1">About Us</p></div></td>
	</table>
</div>
<!-- for the taobao order menu -->
<div class="dropmenu_h1">
     <table class="dropmenu_h1">
	    <td class="dropmenu_h1">

</p>     
            <div class="four" ><p class="menu" > 
             <?php
              if (isset($_COOKIE["uEmail"]))
			     { "<a href='neworder.php'>New Order</a>";}  
		    ?>
      		
			 <?php if (!isset($_COOKIE["uEmail"]))
              echo "New Order";
            ?>
            </p><a href="neworder.php">New Order</a></div></td>
        
        
        
        <td class="dropmenu_h1"><div class="four1"><p class="menu">
         <?php
              if (isset($_COOKIE["uEmail"]))
			     { "<a href='neworder.php'>New Order</a>";}  
		    ?>
      		
			 <?php if (!isset($_COOKIE["uEmail"]))
              echo "Order History";
            ?>
        </div></td>
     </table>
</div> 
<!-- for the payments menu -->
<div class="dropmenu_h2">
      <table class="dropmenu_h2">
         <td class="dropmenu_h2"><div class="five"><p class="menu">Western Union</p></div></td>
         <td class="dropmenu_h2"><div class="five1"><p class="menu">Paypal</p></div></td>
         <td class="dropmenu_h2"><div class="five2"><p class="menu">Bank Transfer</p></div></td>
       </table>
</div> 



<div class="banner">
<p class="banner">
<a href="FirstSite.php"> <?php echo "Taobao Agent Inc."; ?></a>
<p class="login_four"><?php
if (isset($_SESSION['UserId'])){
            
            echo "Welcome " . $_COOKIE["uEmail"] . $_SESSION['UserId']. "!<br />";
}else{
            echo "Please Login!<br />";}
      ?>


</p>



</p>
</div>

</div>
</head>
<div class="divide"></div>
<p>&nbsp;</p>
<p>&nbsp;</p>

</div>
<!--Body container-->
<div class="body_container">
<div class="sidmnu1">

<div class=" loginrect">
<p class="login_one"></p>
<br></br>


<div class="loginrectform">
<form action="validate.php" method="post">

  <p><font color="#000000" size="+2" face="Times">Email:<span id="sprytextfield1">
  <input type="text" name="email"/>
  <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Email address Only</span></span> <br><br/>
Password:<input type="password" name="password"/>
<br><br/>
   <input type="submit"   value="Submit" /> 
   </font>
 <font size="+1"> <a href="userreg.php"> <b> Register </b> </a>  </font>
 </p>
</form>

</div>
</div>
</div>





</div>
 
         

<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "email");
           </script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 04:22 AM

It doesn't work is not a valid description of the problem.

What is this????
         <?php
              if (isset($_COOKIE["uEmail"]))
			     { "<a href='neworder.php'>New Order</a>";}  
		    ?>
      		
			 <?php if (!isset($_COOKIE["uEmail"]))
              echo "Order History";
            ?>


First, why would you ever break out of PHP and then immediately break back into it? Second, what do you think is going to happen to that href???

It's difficult to read your PHP with all that HTML data in it. Try separating your languages.
Was This Post Helpful? 0
  • +
  • -

#9 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 06:39 AM

I'm totally new with PHP, i'm learning it myself.

I am actually trying to control the hyperlink to be shown if the user successfully log in or just show a plain text in the menu if the login wont be successful.

<?php

     if (isset($_COOKIE["uEmail"]))

        { "<a href='neworder.php'>New Order</a>";} 

   ?>


Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 07:14 AM

  • Choose an indent style. White space should exist for a reason, or not be there at all.
  • Understand that PHP and HTML are not the same thing. If you want PHP to send something to the browser you need to use echo or print:
<?php
if (isset($_COOKIE["uEmail"]))
{
   echo "<a href='neworder.php'>New Order</a>";
}
?>


Was This Post Helpful? 0
  • +
  • -

#11 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 07:36 AM

the menu hyper link works perfectly, thanks for the answer,

what about the session i have put session_start at the beginning of the page but i still cant echo it, but i have got an error now

( ! ) Notice: Undefined variable: _SESSION in C:\wamp\www\homework\neworder.php on line 52
Call Stack

I have changed the code to the following

   
        <td class="dropmenu_h1"><div class="four1"><p class="menu">Order History</p>
        
        </div></td>
     </table>
</div> 
<!-- for the payments menu -->
<div class="dropmenu_h2">
      <table class="dropmenu_h2">
         <td class="dropmenu_h2"><div class="five"><p class="menu">Western Union</p></div></td>
         <td class="dropmenu_h2"><div class="five1"><p class="menu">Paypal</p></div></td>
         <td class="dropmenu_h2"><div class="five2"><p class="menu">Bank Transfer</p></div></td>
       </table>
</div> 



<div class="banner">
<p class="banner">
<a href="FirstSite.php"> <?php echo "Taobao Agent Inc."; ?></a>
<p class="login_four">

<?php
if (isset($_COOKIE["uEmail"])){
            $UserId= $_COOKIE["uId"];

            $_SESSION['UserId']=$UserId;
            echo "Welcome " . $_COOKIE["uEmail"] . $_SESSION['UserId']. "!<br />";
}else{
            echo "Please Login!<br />";}

      ?>

</p>



</p>
</div>

</div>
</head>
<div class="divide"></div>




and the new order page to :

<? session_start(); ?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My First Site</title>
<link rel="stylesheet" type="text/css" href="FirstSite.css"></link>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="drop.js"></script>
</head>
<body>
<header>
<!--main container-->
<div class="main_container">
<div class="title">
<div class="drpmnu1">
<table class="drpmnu1">
<td class="drpmnu1"><div class="one"><p class="menu1">Taobao Order</p></div></td>
<td class="drpmnu1"><div class="two"><p class="menu1">Payments</p></div></td>
<td lass="drpmnu1"><div class="three"><p class="menu1">About Us</p></div></td>
</table>
</div>
<!-- for the taobao order menu -->
<div class="dropmenu_h1">
<table class="dropmenu_h1">
<td class="dropmenu_h1">
<div class="four"><p class="menu" ><a href="neworder.php" > New Order </a> </p>


</div></td>



<td class="dropmenu_h1"><div class="four1"><p class="menu">Order History</p></div></td>
</table>
</div>
<!-- for the payments menu -->
<div class="dropmenu_h2">
<table class="dropmenu_h2">
<td class="dropmenu_h2"><div class="five"><p class="menu">Western Union</p></div></td>
<td class="dropmenu_h2"><div class="five1"><p class="menu">Paypal</p></div></td>
<td class="dropmenu_h2"><div class="five2"><p class="menu">Bank Transfer</p></div></td>
</table>
</div>



<div class="banner">
<p class="banner">
<a href="FirstSite.php"> <?php echo "Taobao Agent Inc."; echo $_SESSION['UserId']; ?></a>
</p>
</div>

</div>
</head>
<div class="divide"></div>
<p>&nbsp;</p>
<p>&nbsp;</p>

</div>
Please guide me through. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2952
  • View blog
  • Posts: 10,175
  • Joined: 08-August 08

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 07:37 AM

View PostCTphpnwb, on 25 April 2012 - 07:22 AM, said:

It's difficult to read your PHP with all that HTML data in it. Try separating your languages.

Was This Post Helpful? 0
  • +
  • -

#13 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: problem using $_SESSION to create a super global variable

Posted 25 April 2012 - 07:44 AM

( ! ) Notice: Undefined variable: _SESSION in C:\wamp\www\homework\neworder.php on line 52
Call Stack

I have changed the code to the following

here i generate the SESSION

 


<p class="login_four">

<?php
if (isset($_COOKIE["uEmail"])){
            $UserId= $_COOKIE["uId"];

            $_SESSION['UserId']=$UserId;
            echo "Welcome " . $_COOKIE["uEmail"] . $_SESSION['UserId']. "!<br />";
}else{
            echo "Please Login!<br />";}

      ?>







and the new order.php page i echo the SESSION

[code]

<? session_start(); ?>

<!DOCTYPE html>

...

<div class="banner">
<p class="banner">
<a href="FirstSite.php"> <?php echo "Taobao Agent Inc."; echo $_SESSION['UserId']; ?></a>
</p>
</div>

....

</div>
[/quote]


Please guide me through. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1