You are here: Re: php session variable « PHP Programming Language « IT news, forums, messages
Re: php session variable

Posted by ZeldorBlat on 07/04/07 00:01

On Jul 3, 6:17 pm, Jack <accpac...@hotmail.com> wrote:
> On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
>
>
>
> > On Jul 3, 6:07 pm, Jack <accpac...@hotmail.com> wrote:
>
> > > Hi everyone,
> > > I am stuck on something really stupid and simple. Here is what I am
> > > trying to do. Login, check the username and password against db and if
> > > it checks out then check the logged variable session and pass the user
> > > to the next page. Here is a sample of the login process page:
>
> > > $user = 'test';
> > > $pass = sha1('testpass');
> > > //set the database connection variables
> > > $hostname = "localhost";
> > > $dbusername = "newuser";
> > > $dbpassword = "newpassword";
> > > $dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
> > > die("Unable to connect to the database");
> > > mysql_select_db("seandb", $dbh);
> > > $result = mysql_query("select * from users where username='$user' AND
> > > password='$pass'", $dbh);
>
> > > //check that at least one row was returned
>
> > > $rowCheck = mysql_num_rows($result);
> > > if($rowCheck > 0){
> > > while($row = mysql_fetch_array($result)){
>
> > > //start the session
> > > session_start();
> > > session_register('sname');
> > > $sname = $user;
> > > //successful login code will go here...
> > > header( "Location: main.php" );
> > > }}else {
>
> > > include 'index.php';
> > > printf("Incorrect Login...");
>
> > > }
>
> > > Then on the Main page I want to be able check the variable sname and
> > > if it is set then say hi.
>
> > > For some reason this doesn't happen. The variable gets assigned a
> > > variable but once it is passed over to main.php, the session variable
> > > is not there anymore. Here is the code for main.php:
> > > if (!empty($_SESSION['sname'])) {
> > > ?>
> > > <p>Welcome</p><?php echo($_SESSION['sname']);?>
>
> > > <?php}else{
>
> > > printf("failed...");
> > > //header( "Location: index.php" );
>
> > > }
>
> > > For some reason I always get the "failed" message
>
> > > any ideas...?
>
> > > Thanks
>
> > > J
>
> > Two things:
>
> > 1. Don't use session_register. Just put it in the session array:
> > $_SESSION['sname'] = $user;
>
> > 2. On main.php are you calling session_start() before you do
> > anything? If you aren't, you should be.
>
> I made the changes but still no cigar.
> login.php
> ---------
> while($row = mysql_fetch_array($result)){
>
> //start the session
> session_start();
> $_SESSION['sname'] = $user;
> //successful login code will go here...
> header( "Location: main.php" );
> }
> main.php
> ---------
> session_start();
> if (!empty($_SESSION['sname'])) {
> ?>
> <p>Welcome</p>
>
> <?php}
>
> ?>

Make sure you have cookies enabled. Also make sure error_reporting
and display_errors is enabled so you can see if there are any problems.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация