show example of session code
i m new in using session.
can anyone show me an example of session code? like for example, when the user has entered their information in the fields and when they click onto the submit button, they will be redirected to the next page-with all their information being displayed.
can anyone show me the example of the code?
Re: show example of session code
<?
session_start();
session_register("MyVariableName");
$MyVariableName = 8329;
?>
Re: show example of session code
Mendhak, your code has become obsolete. One should use the $_SESSION array now:
PHP Code:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['sessionVar'] = $_POST['sessionVar'];
}
?>
<html>
<body>
<?php if (isset($_SESSION['sessionVar'])): ?>
<p>You entered <?php echo(htmlspecialchars($_SESSION['sessionVar'])) ?></p>
<?php else : ?>
<form method="post">
<input type="text" name="sessionVar" />
<input type="Submit" name="submit" />
</form>
<?php endif; ?>
</body>
</html>
Re: show example of session code
On a slight tangent, when you handle a POST request you should silently redirect to the intended eventual location using a 303 redirection status code. This instructs the user agent not to cache the response and avoid the irritating "This page contains POSTDATA that has expired from cache" message if you try to go back or refresh the page.