Page 1 of 1

php session

Posted: Sun Sep 10, 2006 11:20 am
by speedy33417
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm new to php sessions. This is my first try at it. I started with a very basic script. My problem is I can echo the value of my variable in the first page, but I get nothing on the second page.
Is the problem in my script or is it a problem with my hosting?

Here's my code:

session1.php

Code: Select all

<html> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
</head> 

<body> 

<?php 
session_start(); 
session_register("color"); 
$color = "blue"; 
echo "The assigned color is: "; 
echo $color; 
?> 

<a href="session2.php">Go to second page</a> 
  
</body> 
</html>
session2.php

Code: Select all

<html> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
</head> 

<body> 
<?php 
session_start(); 
echo "The value stored in your session: "; 
echo $color; 
?> 
</body> 

</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Sep 10, 2006 7:11 pm
by speedy33417
Anyone? Puh-pleeeaaase.

I talked to my hosting company. They do support php scripts. So it's gotta be a bug in the script. Why doesn't my variable have any value once I go to session2.php using the link in session1.php?

Posted: Sun Sep 10, 2006 7:33 pm
by LiveFree
Dont use the the old seesion_register function (depricated)

Instead just use the superglobal $_SESSION var.

Code: Select all

<?php
session_start();
$_SESSION['color'] = "blue";
echo "The assigned color is: ";
echo $_SESSION['color'];
?>
Page 2

Code: Select all

<?php
session_start();
echo "The value stored in your session: ";
echo $_SESSION['color'];
?>

Posted: Sun Sep 10, 2006 9:14 pm
by feyd
For future reference, we don't like bumping, ever. In fact, we have rules concerning it and dictate when and where you can bump. Please observe this in the future.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.

I just went through this

Posted: Mon Sep 11, 2006 2:42 am
by gkwhitworth
The one thing that I learned recently is to not have your session_start() down in the code. You have to send that first. For example:

Code: Select all

<? session_start()
           ?>
<html>
<head>
etc.....
You then do the same on your next page....and you just type in:

Code: Select all

<?
print ("Name: $name");
Hope that helps.

Posted: Tue Sep 12, 2006 2:47 pm
by speedy33417
Thanks. That worked.

session1.php

Code: Select all

<?php
session_start();  
$_SESSION['color'] = "blue"; 
echo "The assigned color is: "; 
echo $_SESSION['color'];
?>
<a href="session2.php">go to second page</a>
session2.php

Code: Select all

<?php 
session_start();
echo "The value stored in your session: ", $_SESSION['color']; 
?>
Now I need to implement it into my log in page.

index.php

Code: Select all

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="inc/theme.css">
</head>

<body leftmargin="0" rightmargin="0" bottommargin="0" topmargin="0" marginheight="0" marginwidth="0">

  <form name="logForm" action="loggedin.php">
    <div align="center">
      <table border="0" width="610" cellpadding="0" cellspacing="0">
        <tr>
          <td width="105" valign="top"><span class="text1">&nbsp;user ID</span></td>
          <td width="105" valign="top"><span class="text1">&nbsp;password</span></td>
          <td width="400"></td>
        </tr>
        <tr>
          <td width="105" valign="top"><input name="userId" class="form3"></td>
          <td width="105" valign="top"><input name="passWord" class="form3"></td>
          <td width="400" valign="bottom"><a href="javascript:if(document.logForm.onsubmit())document.logForm.submit()" class="text1">Log in</a></td>
        </tr>
      </table>
    </div>
  </form>
			
</body>

</html>
My problem is how do I make the session work in this script. My goal would be to store the log in information in userId and passWord and to pass these two variables to the second page: loggedin.php and verify if those variables are correct.

Any help would be great!

Thanks.

Dunno

Posted: Tue Sep 12, 2006 3:21 pm
by gkwhitworth
Buddy as I told you earlier that I am new to php as well. Although I am not new to coding....I would definately use a database for this though. You are talking about a secure section of your site and you need to make sure that not just anyone can have access to the user id's and passwords, especially if there is order info on their member pages. The safest way to pull this off is having a database and placing the whole thing on a SSL server. But, in order for you to get this to work simply, like if you have a member page with the same user id and password you could do the following:

Code: Select all

<? session_start()
?>
<html>
........other coding goes here.......

<?
$userid = $_REQUEST['userid'];
$pass = $_REQUEST['pass'];
$username = array (
            'greg',
            'tom',
            'karen'
            );

if (!isset($_REQUEST['userid'])) {
echo "You need to go to the login and try to login first";
}

if (isset  ($_REQUEST['userid'])) {
    $login_name = $username[$userid];
}

?>

<body>
<?
print ("You Login is correct: $login_name")
?>
Now like I said i am still a newb, so if the code doesn't work, I am sorry but it is just an idea....and it would also be cool to store the user id's and pass in a file and call it up in the beginning if you aren't going to use a db.

Posted: Tue Sep 12, 2006 5:25 pm
by speedy33417
As you can see I'm more new to it than you. :)

I will set up a db for usernames and passwords eventually. Baby steps... Right now I was hoping to use sessions for an html form to process the user input and compare them to manually set variables.

index.php would collect the user inputs (userId and passWord)
loggedin.php would simply compare userId & passWord with (say...) userId[0-9] & passWord[0-9]

Hope it makes sense. :)