Problem with php session

friends
i,m facing a strange problem with session
1- i established my first php website and it is working good on local host and according to the tutorials i had to learn php which i followed them well i used to include this code at the head of each page i need to have a session with it

<?php session_start(); $username=@$_SESSION['username']; echo "$username"; ?>

but when checking my website (online ) not from local host
i get this message
(
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/ansaferc/public_html/mypage.php:3) in /home/ansaferc/public_html/mypage.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ansaferc/public_html/mypage.php:3) in /home/ansaferc/public_html/mypage.php on line 4
)
this is the code i have in the index.php page which allow the user to loging and creat the session for this user
(

                          <!--- login form and php process--->
<?php /*getting login variables */ $username = strip_tags(@$_POST['username']); $userpass = strip_tags(@$_POST['password']); $submit =@$_POST['usersubmit']; if ($submit){ /* Data base connection */ include ("config.php"); /* check for username and password*/ if (!$username==""){ if (!$userpass==""){ $userpass = md5(strip_tags(@$_POST['password'])); $query = mysql_query("SELECT * from commercial_members WHERE username='$username'") or die("couldn,t connect"); $num = mysql_num_rows($query); if ($num!=0){ while($row = mysql_fetch_assoc($query)){ $dbusername=$row['username']; $dbpassword=$row['userpass']; } if ($userpass==$dbpassword){ @$_SESSION['username']=$username; }else {echo " wrong password ";} }else {echo " Unknown user name ";} }else {echo " you must enter password ";} }else {echo " you must enter user name";} } echo" "; ?>

)

so when i login in the index.php page i can see that the session is established but if i (either refresh the page or moved to any other page ) i don,t see that session and i see that message that you,re not logged in plz login or register with us and if i used the back button to go back to the previous page i see thate session again
i tried many time with this problem but it did not work although it is going well on localhost
need your assistance

First why are you suppressing errors? Take the @ symbol off your variables, it’s just going to cause problems down the road. I have no idea where you learn that from, I even had to check what you where doing? :o

Secondly just do the following:

[php]$username = (isset($_SESSION[‘username’])) ? $_SESSION[‘username’] : NULL;[/php]

That way $username won’t throw you an error until you set the $_SESSION variable, btw you still need to start session_start(); first.

Oh, my usual speech, stop using mysql it is deprecated, instead use mysqli or PDO (my recommendation). If still insisting on using mysql at least secure you code.

as for the @ symbol i removed it from variable
i included ($username = (isset($_SESSION[‘username’])) ? $_SESSION[‘username’] : NULL;
)
and the code is this way now
(
session_start();
$username = (isset($_SESSION[‘username’])) ? $_SESSION[‘username’] : NULL;
echo"$username";
) but i still get the same messages i have when i start session and i still don,t see it when refreshing the page or moving to another one
sure there is something wrong

[php]session_start();
$username = (isset($_SESSION[‘username’])) ? $_SESSION[‘username’] : NULL;
echo"$username";[/php]

Do you include this from some other file? if so then you need the session_start() there. It should be of the first lines the web server sees when the request comes in

session_start depends on handling cookies, and that requires being able modify the http headers, which is kinda too late if you have already had html (or any) output.

1 Here is where i include the cod

<?php session_start(); $username = (isset($_SESSION['username'])) ? $_SESSION['username'] : NULL; echo"$username"; ?> In the head section

this form (login form ) is included in every page and session_start function is included tthere too

i don,t understand thae next
( session_start depends on handling cookies, and that requires being able modify the http headers, which is kinda too late if you have already had html (or any) output.)
i know that we need session_start in every page that have a session but how to modify the http header i this the problem is here specially i can see this message in my pages
(
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ansaferc/public_html/index.php:3) in /home/ansaferc/public_html/index.php on line 4)

You cant modify the header info after outputting html. That means no setting cookies after you have output. (Session_start() automagically handles the session cookie for you)

Move session_start() up before any output and you should be ok

With your layout this would mean the beginning of each page.

i moved it at the beginning of the code and even before the start of HTML tags and i kept moving between many pages and it was ok i could see the session every where and it kept saying welcom Mostafa ! and i could manage my own profile and could move to edit it or check any other things related to it from any page
better than before but i still having this message
(Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ansaferc/public_html/index.php:1) in /home/ansaferc/public_html/index.php on line 2 )
line too is where session_start located
1 <?php
2 session_start();

Thank god i did it
i solved this problem as i said that i moved the session_start to the beginning of the page even before the beginning of the HTML tag as shown

1 no space here <?php
2 session_start();
i googled these messages i used to have and i found that there must be no spacebefore the beginnig of the php tags <?php which must be at the beginning of the line as you can see above
so i removed all these spaces in each page and these is no error messages at all and the session is effective in every page it is included
Thank you friends so much
tomorrow i,ll post anothe probles i face but i need to try first to solve it

Sponsor our Newsletter | Privacy Policy | Terms of Service