Session problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cyber_ghost
Forum Newbie
Posts: 1
Joined: Sat May 19, 2007 8:13 am

Session problem

Post by cyber_ghost »

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]


Hi all ! I have a big problem with sessions . I'm trying to do a login system for my site with Sessions . When I login with a username /password I remember the username in a session variable ( $_SESSION['user'] ) . 
In every page i write Hello $_SESSION['user']. The problem is when I navigate on onther pages it tells me 
Hello root . If i login with the username Test it shows me Hello root . It should tell me Hello Test . I'll write down the code .
INDEX.php

Code: Select all

<?php
session_start();
session_destroy();
require_once('connection.php');
include_once('functions.php');
if($_POST['login']){
session_start();
if($_POST['username']!='' && $_POST['password']!=''){
$user=trim(htmlentities(strip_tags($_POST['username'])));
$pass=md5(trim($_POST['password']));
$query="select id from users where username='$user' and password='$pass'";
$doit=mysql_query($query);
if(mysql_affected_rows()==1) {
$_SESSION['user']=$user;
header("location: main.php");exit;
}}}
?>
MAIN.php

Code: Select all

<?
require_once('connection.php');
include_once('functions.php');
session_start();
if(!isset($_SESSION['user'])) header("location: index.php");
$enumMenu=array("1","2","3","4","5","6");
if(!$_GET['page']) $_SESSION['page']='1;
else {
if(in_array($_GET['page'],$enumMenu)) $_SESSION['page']=$_GET['page'];
else $_SESSION['page']='1';
}
if(!$_GET['id']) $id='';
else $id=strip_tags($_GET['id']);
switch ($_SESSION['page']) {
case "1":$what='reg.php';break;
case "2":$what='acasa.php';break;
case "3":$what='articole.php';break;
case "4":$what='newsread.php?id='.$id.'';break;
}
?>
Please help me i have no ideas on what to do to solve the problem . Thank you.


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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

mysql_affected_rows() doesn't return an integer for SELECT queries. Use mysql_num_rows() for the total returned rows. Note that you'll need to pass $doit to mysql_num_rows, as it doesn't automatically fetch the total from the last query.
Post Reply