?
Solved

Session_Start() won't start a session

Posted on 2010-01-04
6
Medium Priority
?
468 Views
Last Modified: 2013-12-12
I have a login page where the user logs in.
Once the login information is verified by authuser.php, the user is taken to the welcome.php page.
On the welcome.php page, there is a link to go to the wk1votes.php page.
On the wk1votes.php page, users will select their votes and submit them to the MySQL database.

My problem is that I am not sure if I need to put session_start() at the top of every page.  Originally I had it like this:
login.html = no session_start()
authuser.php = YES had session_start(), but a new session wouldn't start
welcome.php = no session_start()
wk1votes.php = YES had session_start(), but a new session wouldn't start

Is it necessary for me to have session_start() at the top of every single page?  If not, when should I ensure I add it to the top of the page?
Also, why aren't my sessions starting?  

I am attaching my session_start() code so you can see if it has any errors.

Thank you for your help!


<?php

session_start();
if ($_SESSION[valid] != "yes") {
	header("Location: show_login.html");
	exit;
}

$username = $_COOKIE["auth"];

?>

Open in new window

0
Comment
Question by:deeayrian
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 26177350
HI,

Your code has no error.

Before you can store user information in your PHP session, you must first start up the session.

The session_start() function must appear BEFORE the <html> tag at the top of the page.

This will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

For more information go through:

http://www.w3schools.com/PHP/php_sessions.asp

http://www.tizag.com/phpT/phpsessions.php

Regards.
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26177354
1-Is it necessary for me to have session_start() at the top of every single page?

Yes.


2-Also, why aren't my sessions starting?  

Probably because you are leaving a blank line before session_start();

Must be:
<?php
session_start();
if ($_SESSION[valid] != "yes") {
        header("Location: show_login.html");
        exit;
}

$username = $_COOKIE["auth"];

?>

Open in new window

0
 

Author Comment

by:deeayrian
ID: 26177391
While I was waiting for a response, I decided to remove all of the session_starts from my files to see if the voting process would work properly.  Everything worked perfectly.  Users were able to have their login information verified by the MySQL database, then they were able to submit their votes and add their votes to the database just fine.

I have connection code in my files so that there is a connection established with the MySQL database.  
If I am using cookies in my PHP code, then is it even necessary for me to use session_start()?  I am beginning to think I threw it in there just to complicate things :o)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Accepted Solution

by:
shobinsun earned 2000 total points
ID: 26177439
Hi,

If you are using coockies, no need to use session_start().

Look at this:

http://www.w3schools.com/PHP/php_cookies.asp

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 26177450
Just use:

setcookie(name, value, expire, path, domain);

ex:

setcookie("user", "Something", time()+3600);
0
 

Author Closing Comment

by:deeayrian
ID: 31672762
Thank you for clarifying that I didn't need to use session_start if I was using cookies.  This is where I was confused.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question