Jump to content

Simple Session Question


websponge

Recommended Posts

Im pretty new to PHP, but have managed to create a login system for a small site, my question relates to why Im entering a certain piece of code, I understand the code apart from the getting the information from a form and assigning it to a variable,

 

I have a username form with one submit button, (username) which goes to a script called register.php

 

I have this in my php:

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
$username=$_POST['username'];
session_register("username");
header("location:index2.php");
?>

 

 

then in my index2.php page i have this:

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
session_start();
$username= $_SESSION['username'];
?>
<!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=utf-8" />
<title>My Page</title>
</head>
<body>
<p>
Hi 
<?php
echo  $username; 
?>

</p>
</body>
</html>

Ive simplified what ive done here,why am I declaring a variabel to get the form contents? cant I just get the contents? and then register? I dont need a variable..

 

If I remove the line, it doesnt work obviously..

 

thanks

Link to comment
Share on other sites

Get your form to submit to register.php (like you do) then you should be able to access the username just by using $_POST['username'] if the method for the form is post, and $_GET['username'] if it is set to get. You could do the processing (in this case echoing) on that file, or get your form to submit straight to index2.php and have switch to check if the form was submitted, and if so then do the processing there. There is no real need to set the variable as a session and redirect to a new page.

 

The above code never sets $_SESSION['username'] anywhere, just registers the "username" session

Link to comment
Share on other sites

Thats what I dont understand, I never ask to use that variable again, I have to declare a new one in index2.php

with this line:

$username= $_SESSION['username'];

I dont know why I have to do it twice if you like? sorry its a dull question.. once I get the post details, can I register it on register.php and not do it again on index2.php

Link to comment
Share on other sites

Once the page is redirected variables from the previous page are no longer available. That is why you store variables in sessions in the first place, so you're able to transfer variables across pages.

Link to comment
Share on other sites

Once the page is redirected variables from the previous page are no longer available. That is why you store variables in sessions in the first place, so you're able to transfer variables across pages.

 

Thats my understanding too, so why bother declaring it in the register.php page if it wont get passed to the next page?

Link to comment
Share on other sites

So that it would be defined by session_register, which you shouldn't be using because, as I stated before, it's depreciated. Instead you should do this:

 

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
$_SESSION['username'] = $_POST['username'];
header("location:index2.php");
?>

Link to comment
Share on other sites

So that it would be defined by session_register, which you shouldn't be using because, as I stated before, it's depreciated. Instead you should do this:

 

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); 
$_SESSION['username'] = $_POST['username'];
header("location:index2.php");
?>

 

Tried that, it doesnt display the username, perhaps this is too much for me to learn :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.