Page 1 of 1

php sessions

Posted: Mon May 21, 2007 9:24 am
by var_php
Some one please help me with the following;

I am trying to figure out this whole php thing with regards to eg " making selections of a product/s on different web pages and then adding those products to a "order form" so basically how do i make a php shopping cart , that works using session variables ?


anyone please cause this is going to give me Grey hair !!!!!

PS i have tried to a few tutorials on building a php shopping cart but still have not managed to figure it out or get it working.


*** var_php***

Posted: Mon May 21, 2007 10:57 am
by RobertGonzalez
Moved :arrow: PHP - Code.

Search around the forums here or even on the PHP manual. There are a lot of mentions to sessions and shopping carts around. I would recommend playing with sessions first, before embarking on a Shopping Cart app, or even downloading a free one and using it while you learn what it does by inspecting its code.

Posted: Mon May 21, 2007 12:21 pm
by califdon
Are you trying to learn PHP or do you have the need to use shopping cart technology for a business? In case it's the latter, don't get any more gray hair, just use one of many existing applications, either commercial or open source. If you're trying to learn PHP, I'd suggest starting with a simpler application.

Posted: Tue May 22, 2007 2:26 am
by var_php
Trying to do both , trying to learn php and at the same time build a shopping cart for my site.

I have played endlessly with a few shopping carts for the last 2 months now already but to no avail.

I can grab the concept, it is just implementing the actual code, i understand and can get the cookie to write to the local pc but getting it to accept the newly selected data is another story all together.

Could you suggest a simple cart that i can download and play with, one that explains how to get a form button to POST to the session cookie/SID and then i will go have a look and see if i can get it from there cause the only ones i have found and have tried to build incorporate some extreme measures that are totally confusing me.

Regards

*** var_php***

Posted: Tue May 22, 2007 10:33 am
by RobertGonzalez
If you are using sessions, you can literally store everything in the $_SESSION superglobal array and it will be available to you across your application.

:O)

Posted: Tue May 22, 2007 2:56 pm
by var_php
Thanks Everah

I think let me suck up my pride and wait for my book and then try this cart thing again, but in the mean time work with a few smaller apps.





Thanks again for the help !


var_php :oops:

Posted: Tue May 22, 2007 3:28 pm
by RobertGonzalez
In the meantime you can try a few things. I would say make a few pages and play with them to see how they operate. These are a couple of pages I use for testing every now and again. Play with them as you will...

session-test-page1.php

Code: Select all

<?php
// Session start
session_start();

//Set session vars
if (!isset($_SESSION['username']) && !isset($_SESSION['userage']))
{
    $_SESSION['username'] = 'Guest User';
    $_SESSION['userage'] = 0;
}
else
{
    // Quick error checking
    echo 'Your session details are as follows:<br  />';
    echo '<pre>'; var_dump($_SESSION); echo '</pre>';
    echo '<p>You user name is set to ' . $_SESSION['username'] . '</p>';
    echo '<p>And your age is set to ' . $_SESSION['userage'] . '</p>';
}

// Do we want to clear the session?
if (isset($_GET['clear']))
{
    // Unset all of the session variables.
    $_SESSION = array();

    // If it's desired to kill the session, also delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time()-42000, '/');
    }

    // Finally, destroy the session.
    session_destroy();
    
    echo '<p>Even though you see the information above, the session has already been terminated. 
    <a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '">Refresh the page</a> and you will see nothing as the page sets the session vars again.</p>';
}

// Add some navigation controls
echo '<p><a href="session-test-page2.php">Click here for page 2</a></p>';
echo '<p><a href="?clear=true">Click here to clear the session data</a></p>';
?>
session-test-page2.php

Code: Select all

<?php
//Session start
session_start();

// If the current session is empty, send them to page 1
// This is mostly to make sure default settings are set
if (empty($_SESSION['username']))
{
    header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname(__FILE__) . 'session-test-page1.php');
    exit;
}

// Was the form processed?
if (isset($_POST['change_values']))
{
    // Is the username field not empty?
    if (!empty($_POST['username']))
    {
        $_SESSION['username'] = $_POST['username'];
    }
    
    // Is the age field not empty?
    if (!empty($_POST['userage']))
    {
        $_SESSION['userage'] = intval($_POST['userage']);
    }
}

// Quick error checking
echo 'Your session details are as follows:<br  />';
echo '<pre>'; var_dump($_SESSION); echo '</pre>';

// Echo session vars
echo '<p>You user name is ' . $_SESSION['username'] . '</p>';
echo '<p>And your age is ' . $_SESSION['userage'] . '</p>';

echo '<a href="session-test-page1.php">Click here to go back to page 1</a><br />';

// Added a form to allow users to change test data
echo 'Or enter information into the form below to change the session information.';
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<p>Enter a new username:<br />
<input type="text" name="username" value="<?php echo $_SESSION['username']; ?>" /></p>
<p>Enter a new age:<br />
<input type="text" name="userage" value="<?php echo $_SESSION['userage']; ?>" /></p>
<p><input type="submit" name="change_values" value="Change the session values" /></p>
</form>

sweet !

Posted: Wed May 23, 2007 5:32 am
by var_php
Thanks for the pages :D :D :D :D

Will play with them and see what i can get out of it, really appreciate it Everah , thanks so much :lol: :lol: :lol: :lol:

Re: sweet !

Posted: Thu Jun 14, 2007 9:37 pm
by rozvinbm_jp
var_php wrote:Thanks for the pages :D :D :D :D

Will play with them and see what i can get out of it, really appreciate it Everah , thanks so much :lol: :lol: :lol: :lol:
Please post your resolution for this... It is good to close the topic with resolution.. then others can able to get right away their needs without posting again with the same questions...

Im saying this because I tried to search for my problem but unfortunately there is no any resolution therefore i need to post again a new topic with the same questions... honestly speaking we are the same problem...

Posted: Fri Jun 15, 2007 10:54 am
by RobertGonzalez
@ rozvinbm_jp: Seriously, it is ok to post your own question. Try it, and see if we can't help you without you taking over someone else's thread.