? Nu har jeg ikke den store forståelse for sid.
Her er et udklip til et login jeg bruger men, sid delen forstår jeg ikke helt eller hvad han prøver at fortælle mig i det. det kan være du kan få noget fornuft ud af det.
Når jeg er logget ind så har jeg en sid men vis jeg tager et nyt menu pungt mister jeg den fordi jeg ikke får den tilføjet til linket.
Mit link <a href="?id=333&<?.sid.?>" title="spørsmål">Spørsmål</a>
Men det virker ikke
<?
/* kills the script if the submit button wasn't pressed */
if(!$_POST['submit']){
die("Hacking attempt");
}
#database info, or include a config.php file
$db_host = 'localhost'; #change to your host
$db_user = 'username'; #change to your user name
$db_password = 'password'; #change to your password
$db_name = 'database_name'; #change to your db name
/* Connects to the the database, and selects the database, or kills the script and displays an error that you'll be able to work with */
@mysql_connect($db_host,$db_user,$db_password) or die("Unable to connect to database on line <b>".__LINE__."</b> in <b>".__FILE__."</b>");
@mysql_select_db($db_name) or die("Unable to select to database on line <b>".__LINE__."</b> in <b>".__FILE__."</b>");
/* cleans up the username and pass to work with the database, and to help prevent fraud logins */
$name = trim($_POST['username']);
$password = md5(trim($_POST['user_pass']));
/* kills the script if a username and pass were not entered */
if(!$name || !$password){
die("You need to enter both a username and password before continuing.");
}
/* Searches the database for a user whos id is > 0, change the table prefix if neccessary */
$result = mysql_query("SELECT * FROM phpbb_users WHERE user_id != -1 AND username = '$name' LIMIT 1");
/* Loops through the result to match a user and password, then starts a session to keep them logged in */
while($r=mysql_fetch_assoc($result)){
if(($r['username'] == $name) && ($r['user_password'] == $password) && ($r['user_active'])){
/* Sets the session's name. */
session_name("sid");
/* Starts the session */
session_start();
/* Gets the current session name. */
$s_name = session_name();
/* Gets the current session id, used to track the user from page to page */
$s_id = session_id();
/* Sets the username in the session to be passed from page to page. */
$_SESSION['username'] = $name;
/* Get the current user id, for later use. */
$user_id = $r['user_id'];
/* Get the current time, for later use */
$cur_time = time();
/* Get the user IP, and format it the same way phpBB does (hopefully) */
$user_ip = md5(uniqid($_SERVER['REMOTE_ADDR']));
}elseif(!$r['user_active']){
/* kills the script if the user has not been activated */
die("Sorry, but you must be activated before you can login");
}else{
/* kills the script if something didn't match up above */
die("Invalid username or password. Please try again.");
}
}
/* Checks to make sure the session's username was set, or kills the script */
if(empty($_SESSION['username'])){
die("Username was not set, please try again or contact the site admin.");
}
/* Query used to update the phpBB database, change the prefix of your table if neccessary */
mysql_query("INSERT INTO phpbb_sessions(session_id, session_user_id, session_start, session_time, session_ip, session_logged_in)
VALUES ('$s_id', '$user_id', '$cur_time', '$cur_time', '$user_ip', '1')");
/* Close the mysql connection to help prevent hacking */
mysql_close();
/* This part simply says hi and gives a link back to the index */
echo "You are now logged in as ".$_SESSION['username']."<br>
<a href=/index.php?".$s_name."=".$s_id.">Return Home</a>.";
?>
Now that you're all nice and logged in, how to stay logged in.
First off, you need to change all of your links, to contain the $sid variable, to do so, simply add this to the top of your index.
<?
/* starts a session with the name "sid", used to keep us logged in as long as there is a $sid variable in the url, which we are going to set up next */
session_name("sid");
session_start();
/* this is a simple function that adds the users sid to a url, it's based on the function that phpBB forums acctually use */
function add_sid($url){
/* sets some variables to current sessions name and id */
$s_name = session_name();
$s_id = session_id();
$sid = $s_name."=".$s_id;
if(!defined('LOGGED')){
/* regular url used if the user is not logged in */
return $url;
}else{
/* checks to see if the url already contains the session's variable */
if(preg_match("#$s_name=#", $url)){
/* if it does, return the url */
return $url;
}
/* this is used if the url doesn't have the proper variable, and the user is logged in
it formats the url using something similar to an if statement
visit
http://php.net/manual/en/language.expressions.php for more info */
$url .= ((strpos($url, '?') !== false) ? '&' : '?') . $sid;
/* returns the properly formated url */
return $url;
}
}
?>
Now, how do you use the function? Simply add this below the function, and make the neccessary changes.
<?
/* defines a logged in users name, and sets a constant that says they are logged in */
if(!empty($_SESSION['username'])){
/* Sets the username according to what the session stored */
$username = $_SESSION['username'];
/* defines the constant LOGGED, use this later */
define('LOGGED', true);
}else{
/* default username */
$username = "Guest";
}
/* optional welcome statement */
echo "Welcome $username.";
/* this is a sample link, you don't need it, its here for educational purposes. */
echo "<a href=".add_sid("/short/link/to_page.php").">DISPLAYED_PART</a>";
?>
Ok, we defined a nice constant, and set some variables up, all for what you ask? Well, heres something you can do with the constant (i.e. for members-only features?).
<?
if(defined('LOGGED')){
echo "You get members only features. YAY!";
}else{
echo "Hey, go register and login for members only features!!";
}
?>