Jump to content

Recommended Posts

I've viewed so many different tutorials etc. and made so many codes that I actually now don't understand some of my code. Please do take a look:

 

<?php

session_start();
if(!session_is_registered(myusername)) {
header("location:index.php");
}

 

The thing that confuses me is "myusername" Where the hell is it coming from? I can't remember why I put it there when I first did. And now it's confusing me to pieces! It's nothing in the database, it's not a variable, and it's not the name of some html stuff either. So what is it??

 

Thanks guys.

myusername is nothing probably some code from some tutorial where it made sense. And like thorpe said it's deprecated use:

 

session_start();
if (!isset($_SESSION['myusername'])) {
    $_SESSION['myusername'] = $username;
}

 

Edit: deprecated means it is no longer supported and will be removed in a future version.

 

Wikipedia puts it in this way:

the term deprecation is applied to software features that are superseded and should be avoided.

Here is what I use for my login script hope it helps $username comes from the database

 

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

 

mysuername just probably comes from a tutorial or something where you may have copied and pasted just find out what they are supposed to log in under or what have you or define myusernam.

Firstly; a quote from PHP.net

 

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

 

It should be done like so:

 

session_start();
if(!isset($_SESSION['myusername'])) {
      header("location:index.php");
}

 

Check the part of your script where you log a user in... If you're not setting it there, then you've made a typo or something.

So is "myusername" something I have specified?if so where? Cos I've tried editing it to 'username' but that doesn't work... For some reason it has to be "myusername"

 

Yes, and how should we know.

 

It means that you have likely registered an index of 'myusername' within the $_SERSSION array somewhere.

So even though "myusername" was specified in a DIFFERENT php file it still finds it? I certainly havent specified "myusername" in the php file i took the code out from. It's just the code I paste in to make users not be able to go to the pages if they are not logged in.

Ok, I checked my checklogin.php file and I found this:

 

$count = mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}

 

Is that where I specified it?

Also; please note that you're not actually giving the session variable "myusername" any value. You're just setting it as a variable. To give it the actual username of the user, you'll have to take the username out of the result set you got from MySQL and hand the username over to $_SESSION['myusername']

<?




$host = "localhost";
$username = "zoroaste";
$password = "****";
$db_name = "zoroaste_test1";
$tbl_name = "Login";

mysql_connect($host, $username, $password) or die("Failed to connect to mysql!");
mysql_select_db($db_name) or die ("Failed to connect to mysql!");

$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

Alright this is the whole checklogin.php file. I'm still confused. Where am I specifying myusername and what am i specifying it to? I know that it's deprecated, but lets just look away from that.

 

I'm trying to make a list of all the registered members post itself on the site. And this is confusing me. I can only manage to make it show the user you're logged in to.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.