0

I was planning to create guest login which composed of

  • nickname
  • button submit

The problem: If the user1 choose his nickname as "KAKA", the other user can't use the nickname "KAKA"(In other browser) because its was already been taken.

The scenario goes like this.

In a browser, if user1 will have to login, it will be stored his nickname into a session variable.

On the other side.If user2 wants to login (In other browser), she/he can't login because of the same nickname.

Sume-mase
  • 119
  • 8
  • You can put the temporary entry in table and delete it at login. – Harshal Apr 04 '15 at 08:36
  • what about your database?? – habib ul haq Apr 04 '15 at 08:36
  • No need to use database for this, just a temporary variable. I just wanna compare both sessions in different browsers. – Sume-mase Apr 04 '15 at 08:38
  • No.. Session details are stored per brower.. How about using Local Storage?(http://stackoverflow.com/questions/23884805/local-storage-jquery) – Arun Apr 04 '15 at 08:39
  • If that's the case, how can I solve? – Sume-mase Apr 04 '15 at 08:41
  • Session in maintained in the form of cookies on client end. so you can not compare all the session values with different browsers. You have to take the help of database. – Harshal Apr 04 '15 at 08:43
  • So, every time the user_guest will attemp to login will be stored in table?So what's the purpose of the session.I think it's useless. @HarshalMahajan – Sume-mase Apr 04 '15 at 08:50
  • You just need to store the Username in table , to make sure the users could not get logged in with same user id.This is the only purpose to use the table. – Harshal Apr 04 '15 at 08:56

3 Answers3

1

To Avoid the Duplicate record in your session. You may use Database for such scnerio.

You can check into the database if the user name is exist.Like:

$temp_username = $_POST['username'];//Get the username
if($this->Is_username_available($temp_username))//call the function and check from table that the user name is available. 
{
    //Create session with posted username.
}
else
{
  // show message that Username is already taken for current session.
}

On Logout :

Delete_Username($_SESSION['username']);//or you may take $_SESSION['username_id'] if you stored at the time of creation.
Harshal
  • 3,562
  • 9
  • 36
  • 65
  • The other way to solve this is to create table? But do I really need to store the session of the guest in a table?. I was referring to a GUEST user only. I think no need to store the session. The guest can't login as the same nickname of the others in a different browsers – Sume-mase Apr 04 '15 at 08:48
  • No you don't need to store session in table , just put the requested username in table and check on each request for uniqueness. – Harshal Apr 04 '15 at 08:49
  • @Sume-mase, sessions are browser dependant there is no way to retrieve data between session by default. This means you will need to store the used nicknames in a central place which you can access so you can validate new entires. The easiest solution for this is to create a table to store the users. Some other methods are using memcache or storing the sessions in the database instead of the default method (in files on the server) – DarkBee Apr 04 '15 at 10:18
  • This needs to part of the session management and must be implemented within the session handler. Sessions timeout as well as users logging out. – symcbean Apr 04 '15 at 10:19
0

The only efficient way to do this is to use Database. Because sessions are independent of the server.They are established on client side(i.e. browser). They are created in the browsers by getting data from the server. Each browser is having its own copy of session. That is all browser's sessions are independent of other. There is no communication of sessions between browsers possible without the support of any server side language.

For your task, proceed as follows:

  • On successful login, store the nickname in the table as well as in Session(to keep user logged in).
  • Set time out for current user to expire his/her session and after timeout, delete the table entry too. (This is required in case if a user logged in and never log out, no other user with same nickname will be able to log in)
  • Before logging in, first check the nickname entered by the user is present in the table or not.
  • If the nickname exists in the table, reject the login of this user because another user with this nickname is already logged in.
  • During logout phase, delete the nickname from table and clear the session to make user successfully log out.
Choxx
  • 945
  • 1
  • 24
  • 46
  • 1
    You forgot to time out guests when they don't logout, causing a name to be locked forever – DarkBee Apr 04 '15 at 10:14
  • @DarkBee Thanks to let me know that. I copied that in post. – Choxx Apr 04 '15 at 10:21
  • Good point, but how can I delete the table after timeout when never logout? @choxx – Sume-mase Apr 04 '15 at 11:31
  • Visit this link http://snipplr.com/view/72803/how-to-expire-a-php-session-after-x-minutes/ and this for more detailed explanation http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – Choxx Apr 04 '15 at 11:35
  • @Sume-mase you can also use cookies instead of sessions which have default capability for expiration(or we can say timeout) and this will also works same as session for you. – Choxx Apr 04 '15 at 11:39
  • @Sume-mase one more thing you can do if you are using javascript in your project. Use **setTimeout()** method which calls a function or evaluates an expression after a specified number of milliseconds. Set the required time in milliseconds and and place the session unset code in its definition. For more info on this method, visit http://www.w3schools.com/jsref/met_win_settimeout.asp And to delete values from table, you will have to use ajax along with js cos js is client side. – Choxx Apr 04 '15 at 11:44
  • Thanks! more power :) – Sume-mase Apr 04 '15 at 11:49
-3

you may use a random string function for a separate sessions. Store the combination of nickname & string to communicate with the log in. In comparison not only the nickname will be compared but the random string too for allowing same nickname to others with a different random string...

lakshman
  • 656
  • 4
  • 18
  • And can you explain me how will you check that nickname with random string on different browsers are matching or not? (Keep in mind that browsers are different and reply then) – Choxx Apr 04 '15 at 09:47
  • Yeah..store the values in table and use for comparison or create a file in txt or xml to write the active session if you are not using database – lakshman Apr 06 '15 at 02:56
  • Now you are almost correct.. But still, go with database, other things will create complexity.. – Choxx Apr 06 '15 at 03:02