Jump to content

Recommended Posts

Hi Guys,

 

This question is partly about Javascript too but you probably know all about it too. I am trying to use a php script with mysql database to authenticate users and set a cookie. Basically i need a members site

 

I currently have my database which is perfectly fine at the moment.

 

Then i have 2 php scripts one is a login and one is registration. I have them as seperate scripts ie login.php and register.php both saved on the server and the index.html page has each of them in an iframe. They both seem to work ok but the only problem is what do i do when i have authenticated an set the cookie? i cant set a header as the script running it is in an iframe! could i send a refresh command or send an f5 key? What is the best way to do this?

 

I also have login/logout part but i want to run it in javascript so that if a cookie is set, it shows logout and if pressed unsets the cookie (called user) and if no cookie is set i want it to display login and forward to a page.... login.html? or something not made it yet.

I tried writing a script and placing it on the page in a script box (Sitespinner) but it doesnt work:

 

<html>
<head>
<script type="text/javascript">

function checkCookie()

{
username=getCookie('user');
if (username!=null && username!="")
{
alert('Welcome again '+user+'!');
}
} 

</script>
</head>
<body onLoad="checkCookie()">

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/133697-page-error-login-script/
Share on other sites

1) Why are you asking a Javascript question in the PHP Help section?

2) Why don't you use PHP to check the cookies?

 



if (isset($_COOKIE["user"]))
  echo "Welcome " . $_COOKIE["user"] . "!
";
else
  echo "Welcome guest!
";
?>


 

Check out w3schools - cookies for a full explanation.



if (isset($_COOKIE["user"]))
  echo "Welcome " . $_COOKIE["user"] . "!
";
  $user = $_COOKIE["user"];
  echo "Logout"
else
  echo "Welcome guest!
";
?>


 

 

If the user clicks logout it goes to the logout.php page where you can create your own logout logic (unset the session, turn him offline in your DB, w/e).  To retrieve the user name just use $_GET['user'];  Does this help?

After that i have eventually used a flash actionscript to post the login details to the database and then set a cookie if authorised and used the header() function to send the user back to the loginpage where there is a script that reads if there is a cookie set and if so shows the welcome message and a logout link, But.... when the user is authorised and sent back to the login page the logout script doesnt pick up the fact that there is a new cookie until the refresh key is hit! why is this?

www.foxmart.co.uk/index.html

test with:

 

User Email:    test@test.com

password:    test

 

Thanks

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.