Foros del Web » Programando para Internet » PHP »

Problema con varias sesiones en PHP

Estas en el tema de Problema con varias sesiones en PHP en el foro de PHP en Foros del Web. Hola! Tengo una duda con el tema de sesinoes en PHP que no sé solucionar. Estoy programando una tienda con Dreamweaver y tengo una sesión ...
  #1 (permalink)  
Antiguo 06/03/2013, 07:05
 
Fecha de Ingreso: mayo-2011
Mensajes: 9
Antigüedad: 12 años, 11 meses
Puntos: 0
Problema con varias sesiones en PHP

Hola!
Tengo una duda con el tema de sesinoes en PHP que no sé solucionar.
Estoy programando una tienda con Dreamweaver y tengo una sesión llamada "carrito" que me guarda los datos seleccionados (nombre del producto y precio del producto) y los coloca en la página resumen.php De esta manera cuando el comprador vuelve a trás puede seleccinoar otros productos y se van sumando. Hasta aquí bien.
Lo que quisiera hacer es que cuando el comprador ya tiene sus productos seleccionados y vaya a comprar, tenga que loggearse para que asocie sus datos a los productos seleccionados. Esto lo hago con la opción 'Conectar usuario' de Dreamweraver.
El problema es que cuando hago el link para que se loggeen y vuelvo para atrás, los productos seleccinoados en la sesión "carrito" se pierden y entonces el comprador debería volver a seleccinoarlos.
¿Existe alguna forma de que permanezcan estos datos cuando el usuario inicie su sesión?

Estos son los archivos utilizados: resumen.php e iniciarsesion.php:

resumen.php:
Código PHP:
<?php require_once('../../Connections/compra.php'); ?>
<?
session_start
();
if(isset(
$_SESSION['carrito'])){
    
$carrito_mio=$_SESSION['carrito'];
}

?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

mysql_select_db($database_ocolor$ocolor);
$query_r_noticias "SELECT *,DATE_FORMAT(fecha_noticias,'%d-%m-%Y') AS fecha_noticias FROM noticias";
$r_noticias mysql_query($query_r_noticias$ocolor) or die(mysql_error());
$row_r_noticias mysql_fetch_assoc($r_noticias);
$totalRows_r_noticias mysql_num_rows($r_noticias);

mysql_select_db($database_ocolor$ocolor);
$query_r_usuarios "SELECT * FROM usuarios";
$r_usuarios mysql_query($query_r_usuarios$ocolor) or die(mysql_error());
$row_r_usuarios mysql_fetch_assoc($r_usuarios);
$totalRows_r_usuarios mysql_num_rows($r_usuarios);

$colname_r_categorias "-1";
if (isset(
$_GET['id_categorias'])) {
  
$colname_r_categorias $_GET['id_categorias'];
}

$colname_r_productos "-1";
if (isset(
$_GET['id_categorias'])) {
  
$colname_r_productos $_GET['id_categorias'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Tienda Online</title>
<link href="../../Css/estilos.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<link href="../../Css/estilos.css" rel="stylesheet" type="text/css" />
<style type="text/css"></style>
</head>
<body>
<div id="contenedor">
      <div id="centro">
        <form action="../../Wlibres/final.php" method="post">
        <table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td>
                  <table width="550" border="0" align="center" cellpadding="0" cellspacing="0" id="tb_result">
                <tr>
                  <td height="20" colspan="8">&nbsp;</td>
                  </tr>
                <tr>
                  <td height="20" colspan="8" align="center"><span class="Estilo8">Resumen del pedido </span></td>
                </tr>
                <tr>
                  <td height="20" colspan="8"><span class="Estilo1"></span></td>
                </tr>
                <tr>
                  <td width="5" bgcolor="#C5C5C5"><span class="Estilo1"></span></td>
                      <td width="202" height="20" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Producto</strong></span></td>
                      <td width="82" height="20" align="center" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Marca</strong></span></td>
                      <td width="76" height="20" align="center" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Referencia</strong></span></td>
                      <td width="54" height="20" align="right" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Precio</strong></span></td>
                      <td width="69" height="20" align="center" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Cantidad</strong></span></td>
                      <td width="57" height="20" align="right" bgcolor="#C5C5C5"><span class="Estilo5 Estilo1"><strong>Total</strong></span></td>
                       <td width="5" align="right" bgcolor="#C5C5C5"><span class="Estilo1"></span></td>
                </tr>
                <?
                
if(isset($_SESSION['carrito'])){
                
$total=0;
                for(
$i=0;$i<=count($carrito_mio)-1;$i ++){
                if(
$carrito_mio[$i]!=NULL){
                
?>
                <tr>
                  <td width="5" valign="middle" bgcolor="#E1E1E1">&nbsp;</td>
                      <td height="50" valign="middle" bgcolor="#E1E1E1"><span class="Estilo8"><? print $carrito_mio[$i]['nombre_productos'];   ?></span></td>
                     <td height="50" align="center" valign="middle" bgcolor="#E1E1E1" class="Estilo1"><span class="Estilo1"><? print $carrito_mio[$i]['nombre_marcas'];   ?></span></td>
                     <td height="50" align="center" valign="middle" bgcolor="#E1E1E1" class="Estilo1"><span class="Estilo1"><? print $carrito_mio[$i]['referencia_productos'];   ?></span></td>
                     <td height="50" align="right" valign="middle" bgcolor="#E1E1E1" class="Estilo1"><span class="Estilo1"><? print $carrito_mio[$i]['precioparticular_productos'];   ?></span></td>
                      <td height="50" align="right" valign="middle" bgcolor="#E1E1E1" class="Estilo1"><span class="Estilo1"><? print $carrito_mio[$i]['cantidad'];   ?></span></td>
                      <td height="50" align="right" valign="middle" bgcolor="#E1E1E1" class="Estilo1"><span class="Estilo8">
                         <?  print $carrito_mio[$i]['precioparticular_productos'] * $carrito_mio[$i]['cantidad'];  ?>
                         <strong>&euro;</strong>
                       </span></td>
                       <td width="5" align="right" valign="middle" bgcolor="#E1E1E1" class="Estilo1">&nbsp;</td>
                </tr>
                <?
                $total
=$total + ($carrito_mio[$i]['precioparticular_productos'] * $carrito_mio[$i]['cantidad']);
                }
                }
                }
                
?>
                <tr>
                  <td colspan="8">&nbsp;</td>
                  </tr>
                <tr>
                      <td colspan="4"><span class="Estilo11"></span></td>
                      <td>&nbsp;</td>
                      <td align="left"><span class="Estilo12"><strong>Total:</strong></span></td>
                      <td colspan="2" align="right"><span class="Estilo12"><strong>
                       <? 
                      
if(isset($_SESSION['carrito'])){
                      print 
$total
                      }
                       
?> 
                       &euro; </strong></span></td>
                     </tr>
            </table>
              
              </td>
            </tr>
            </table>
        </form>
          <input type="submit" name="Submit24" value="Ya estoy registrado" onclick="window.location='../../Usuarios/iniciarsesion.php'"/>
          <input type="submit" name="Submit222" value="Registrarme" onclick="window.location='../../Usuarios/registrarse.php'"/>
          <input type="submit" name="Submit232" value="Comprar sin registrarme" onclick="window.location='../../Wlibres/final2.php'"/>
        </div>
</div>
</body>
</html>
<?php
mysql_free_result
($r_noticias);

mysql_free_result($r_usuarios);
?>
A continuación adjunto archivo iniciarsesion.php!!!
  #2 (permalink)  
Antiguo 06/03/2013, 07:06
 
Fecha de Ingreso: mayo-2011
Mensajes: 9
Antigüedad: 12 años, 11 meses
Puntos: 0
Respuesta: Problema con varias sesiones en PHP

iniciarsesion.php:
Código PHP:
<?php require_once('../Connections/compra.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

mysql_select_db($database_ocolor$ocolor);
$query_r_noticias "SELECT *,DATE_FORMAT(fecha_noticias,'%d-%m-%Y') AS fecha_noticias FROM noticias";
$r_noticias mysql_query($query_r_noticias$ocolor) or die(mysql_error());
$row_r_noticias mysql_fetch_assoc($r_noticias);
$totalRows_r_noticias mysql_num_rows($r_noticias);

mysql_select_db($database_ocolor$ocolor);
$query_r_usuarios "SELECT * FROM usuarios";
$r_usuarios mysql_query($query_r_usuarios$ocolor) or die(mysql_error());
$row_r_usuarios mysql_fetch_assoc($r_usuarios);
$totalRows_r_usuarios mysql_num_rows($r_usuarios);

$colname_r_categorias "-1";
if (isset(
$_GET['id_categorias'])) {
  
$colname_r_categorias $_GET['id_categorias'];
}

$colname_r_productos "-1";
if (isset(
$_GET['id_categorias'])) {
  
$colname_r_productos $_GET['id_categorias'];
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  
session_start();
}

$loginFormAction $_SERVER['PHP_SELF'];
if (isset(
$_GET['accesscheck'])) {
  
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset(
$_POST['textfield'])) {
  
$loginUsername=$_POST['textfield'];
  
$password=$_POST['textfield2'];
  
$MM_fldUserAuthorization "tipo_usuarios";
  
$MM_redirectLoginSuccess "redirigir.php";
  
$MM_redirectLoginFailed "iniciarsesionfallo.php";
  
$MM_redirecttoReferrer false;
  
mysql_select_db($database_ocolor$ocolor);
      
  
$LoginRS__query=sprintf("SELECT nombreusuario_usuarios, contrasena_usuarios, tipo_usuarios FROM usuarios WHERE nombreusuario_usuarios=%s AND contrasena_usuarios=%s",
  
GetSQLValueString($loginUsername"text"), GetSQLValueString($password"text")); 
   
  
$LoginRS mysql_query($LoginRS__query$ocolor) or die(mysql_error());
  
$loginFoundUser mysql_num_rows($LoginRS);
  if (
$loginFoundUser) {
    
    
$loginStrGroup  mysql_result($LoginRS,0,'tipo_usuarios');
    
    
//declare two session variables and assign them
    
$_SESSION['MM_Username'] = $loginUsername;
    
$_SESSION['MM_UserGroup'] = $loginStrGroup;          

    if (isset(
$_SESSION['PrevUrl']) && false) {
      
$MM_redirectLoginSuccess $_SESSION['PrevUrl'];    
    }
    
header("Location: " $MM_redirectLoginSuccess );
  }
  else {
    
header("Location: "$MM_redirectLoginFailed );
  }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Orbicolor.com</title>
    <link href="../Css/estilos.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    </style>
    <link href="../Css/estilos.css" rel="stylesheet" type="text/css" />
    <script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
    <link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
    <style type="text/css"></style>
</head>
<body>
<div id="contenedor">
    <div id="centro"><table width="550" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td height="15">&nbsp;</td>
            </tr>
            <tr>
            <td><strong>INICIAR SESI&Oacute;N</strong></td>
            </tr>
          <tr>
            <td><hr /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><form ACTION="<?php echo $loginFormAction?>" id="form2" name="form2" method="POST">
              <table width="550" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="202" align="right"><span class="style8">Nombre de usuario:&nbsp;&nbsp; </span></td>
                  <td width="348"><span class="style6" id="sprytextfield1">
                    <label></label>
                    </span><span id="sprytextfield3">
                    <label>
                     <input type="text" name="textfield" id="textfield" />
                    </label>
                    <span class="textfieldRequiredMsg">Campo obligatorio</span></span></td>
                </tr>
                <tr>
                  <td align="right">&nbsp;</td>
                  <td><span class="style6"></span></td>
                </tr>
                <tr>
                  <td align="right"><span class="style8">Contrase&ntilde;a:&nbsp;&nbsp;</span></td>
                  <td><span id="sprytextfield2">
                    <label>
                    <input type="password" name="textfield2" id="textfield2" />
                    </label>
                    <span class="textfieldRequiredMsg">Campo obligatorio</span></span></td>
                </tr>
                <tr>
                  <td align="right">&nbsp;</td>
                  <td><span class="style6"></span></td>
                </tr>
                <tr>
                  <td align="right">&nbsp;</td>
                  <td><span class="style6">
                    <label>
                    <input type="submit" name="button2" id="button2" value="Iniciar Sesi&oacute;n" />
                    </label>
                  </span></td>
                </tr>
              </table>
              </form>    </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table>
    </div>
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
//-->
</script>
</div>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result
($r_noticias);

mysql_free_result($r_usuarios);
?>

Muchas gracias y un saludo!

Etiquetas: login, sesión, sesiones
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:27.