Solved

Session from normal page to a function

Posted on 2004-10-18
5
179 Views
Last Modified: 2006-11-17
Hello,

I am using a session like:
<?php
session_start();
session_register("list");

if (!IsSet($list)) {
      $list = "";
}
$split = explode(",",$list);
if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }
      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}
?>


Now i want to put it in a function. I tryed this:
function content()
{
db_connect();

global $list;

session_start();
session_register("list");

if (!IsSet($list)) {
      $list = "";
}
$split = explode(",",$list);
if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }
      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}
?>

I want to read the session in an other function how to do??? or what do i do wronge???

HP van Hagen
WvR
0
Comment
Question by:WvR
5 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12337377
session_start(); should be outside of the function, and you are using the old way to register sessions, they are super globals anyway so dont need making global.

Add session_start(); at the top of your page before output print/echo/html tags/new lines before <?php

then within your function just reference $_SESSION['list'] and thats it

$_SESSION['list'] will be available inside and outside of the function as its scope extends to both.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 total points
ID: 12337495
This is perhaps something like what you will want:

session_start();

function content() {
 db_connect();

 if (!isset($_SESSION['list'])) $_SESSION['list'] = "";
 
 if ($_SESSION['list'] == "") {
  $_SESSION['list'] = "$id+$amount";
 }
 else {
  $split = explode(",", $_SESSION['list']);
  foreach ($split as $item) {
   $check = explode("+", $item);
   if ($check[0] == $id) {
    $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
    $replace = "ok";
   }
  }
 }
 if (!isset($replace)) $list = "$list,$id+$amount";
}

Although there are still flaws, for example (referring to your original code)
$list = "$id+$amount"; $id and $amount don't exist in this scope so if there have been created outside the function you must make them global so they are available inside otherwise you must feed them to the function like this:

First line of function: function content($id,$amount) {

when you call function (for example): content(10013,2); in otherwords content(the_id,the_amount);
0
 
LVL 1

Author Comment

by:WvR
ID: 12337810
Sorry i am a starter her.

What i do is, from detail.php i go to order.php
Like this for detail.php:

<?php
//---INCLUDES-------------------------------------------------------------------
require_once("includes/app_common_inc.php");
//---HOOFDPROGRAMMA-------------------------------------------------------------
page_header();
menu_links();

session_start();
content();

end_page();
prevent_caching();

//------------------------------------------------------------------------------
//---PAGINA-SPECIEKE FUNCTIES---------------------------------------------------
//------------------------------------------------------------------------------
function content()
{
db_connect();

$id   = $_GET["id"];
$query = mysql_query("SELECT * FROM voorbeeld_product WHERE id='$id'");
$row = mysql_fetch_row($query);
?>
<table border="0" width="335">
<tr>
<?php

      if ($row[7] != "none") {
            $split = explode("../", $row[7]);
            $row[7] = $split[1];
            $img = "<img src=\"$row[7]\">";
      } else {
            $img = "<img src=\"images/nb.jpg\">";
      }

?>
      <td class="standard" colspan="2"><? echo "$img"; ?></td>
</tr><tr>
      <td class="standard" valign="top"><b>Naam: </b></td>
      <td class="standard"><? echo "$row[3]"; ?></td>
</tr><tr>
      <td class="standard" valign="top"><b>Specificatie: </b></td>
      <td class="standard"><? echo "$row[5]"; ?></td>
</tr><tr>
      <td class="standard"><b>Prijs: </b></td>
      <td class="standard">Euro: <?php echo "$row[4]"; ?></td>
</tr>
</table>

<form action="order.php" method="post">
<input type="hidden" value="<?php echo "$row[0]"; ?>" name="id">

<table border="0">
<tr>
      <td class="standard">Bedrag: </td>
      <td class="standard"><input type="text" size="3" name="amount" value="1" class="input"></td>
      <td class="standard" colspan="2"><input type="submit" value="Bestel" class="input"></td>
</tr>
</table>
</form>

<?php
db_close();
}
// end function content
//------------------------------------------------------------------------------
?>




Order.php looks like this:

<?php
//---INCLUDES-------------------------------------------------------------------
require_once("includes/app_common_inc.php");
//---HOOFDPROGRAMMA-------------------------------------------------------------
page_header();
menu_links();

session_start();
content();

end_page();
prevent_caching();

//------------------------------------------------------------------------------
//---PAGINA-SPECIEKE FUNCTIES---------------------------------------------------
//------------------------------------------------------------------------------
function content()
{
db_connect();
global $list;

$id      = $_POST["id"];
$amount      = $_POST["amount"];

session_register("list");

if (!IsSet($list)) {
      $list = "";
}

$split = explode(",",$list);

if ($list == "") {
      $list = "$id+$amount";
} else {
      $split = explode(",", $list);
      foreach ($split as $item) {
            $check = explode("+", $item);

            if ($check[0] == $id) {
                  $list = str_replace("$check[0]+$check[1]","$check[0]+$amount",$list);
                  $replace = "ok";
            }
      }

      if (!IsSet($replace)) {
            $list = "$list,$id+$amount";
      }
}

$query = mysql_query("SELECT * FROM voorbeeld_hoofdcat");
?>

<center>
<br><br>
<table border="0">
 <tr>
   <td><a href="cart.php" class="standard">Pay</a></td></tr><br><br>
   <tr><td><a href="maincat.php" class="standard">Go on</a></td><br>
 </tr>
</table>
<?php
db_close();
}
// end function content
//------------------------------------------------------------------------------
?>


As you can see i want to go further with cart.php or maincat.php

What do i do wrong???

HP van Hagen
WvR
0
 
LVL 20

Expert Comment

by:virmaior
ID: 12338144
for one thing, you should call session_start() before the page headers...  depending on your php version, it might not work properly if there's been any output prior to session_start..
you should probably put it at the front of your first included file...
0
 
LVL 2

Expert Comment

by:TheUnkind
ID: 12355737
Hi, Try using $GLOBALS[""] instead of using $variable  inside your function

Hope this helps...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now