Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

PHP sessions

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hey again. Encountered new problem. Name: sessions. I've tried Google and it led me to about 4 (in total) tutorials. About 2 of which looked as if they were typed by 5 year olds and the other 2 were the opposite. Trying to sound very technological and not speaking a language I understand (in more ways than one) because they were using pear and I need MySql.

Anyway. I basically have a database named 'users' with usernames, passwords, emails, etc. I also have a registration form and a login form which work perfectly. However, I need to introduce 'sessions' and it isn't going to well. I've been told to include 'session_start()' but not one tutorial told me WHERE to put this but still. I want the user to stay 'logged in' and to have their own profile.

Sorry, I've had no sleep and I'm very frustrated with time wasting tutorials but I know I can rely on you lot to help me out and cheer me up. Thanks
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Put it first thing in each page...

<?php

session_start();

//rest of your code here

?>

Make sure it comes first for each page that you use sessions.
 
0
Close
Thanks. Do sites like ebay use cookies or sessions (or both)?
 
0
Close
miseria said:
Thanks. Do sites like ebay use cookies or sessions (or both)?


ebay uses cookies, passwords are in MD5();
 
0
Close
Oh right. I think I'll stick with sessions anyway. When do I declare the session (if that makes sense)? I have the following

PHP:
$login_check = mysql_num_rows($sql);
if ($login_check == "1") {
include ("membersarea.php");
}

would it be...

PHP:
$login_check = mysql_num_rows($sql);
if ($login_check == "1") {
?SESSION STUFF HERE?
include ("membersarea.php");
}
 
0
Close
As long as session_start() goes before any other headers are sent, then it will work.

To be safe, just put session_start() at the beginning of your PHP in each file that uses sessions.
 
0
Close
PHP:
<?php
session_start();
if(isset($_POST['submit'])) {

// check from database

$login_check = mysql_num_rows($sql); 
if ($login_check == "1") { 
header("Location: members.php");
} else {
header("Location: error.php?e=1234");
}
}
and error.php ?e=1234 redirects it to the badd password as example
 
0
Close
Thanks a lot peoples. How do I relate a session to MySQL if it's possible. So that when someone signs in with the session and are redirected to the member's page, is it possible to display their information from the database?

EDIT: Could I use queries with the username they signed in with?
 
0
Close
PHP:
<?php
session_start();
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
	$password = $_SESSION['password'];
	$username = $_SESSION['username'];
	//
	$query="SELECT * FROM users WHERE username='$usenrame'";
	$sql = mysql_query($query)or die(msql_error());
	$obj = mysql_fetch_object($sql);
	$email = $obj->email;
	$member_id = $obj->id;
	$member_name = $obj->username;
	//
	}
	if (!isset($_SESSION['username']) or !isset($_SESSION['password'])) {
	header("Location : http://namepros.com");
	}
}
?>

name it what ever you want, and include it in all members pages
and then you echo the information from the database like this:

example :
PHP:
echo $email;
 
0
Close
Thank you so much
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back