Just getting my grounds with PHP and running into trouble with cookies/sessions.

Now before I start I must exclaim two things.

  1. I am a dyslexic. I will do my best to keep my spelling and grammar neat but you will more then likely notice plenty of mistakes. Sorry.
  2. I am as green as can be with PHP, or with most code languages that is… (Green meaning new) I have gotten some grounds with C and C++. But as for HTML and CSS (which I don’t consider real code languages) I’m fairly good with them, and am decent with java too. but any “programing” language, I really have yet to get my grounds with them.

So I’ll explain my situation. Just before summers end I made a basic game website. Fairly basic layout and currently not allot of games but it gets the job done, and I get some money from the adds I post on it. It gets quite a bit of traffic from all the students at my school visiting it, sometimes as many as 300 views a day. But there is something I had an idea for but have yet, failed to do because of my failure to understand how to get PHP in the middle of HTML. (Meaning in the middle of HTML code, not in the middle of an HTML file.) Now I should explain what I’m talking about first. There was one website that I use to regular when I was in elementary school, that has since then shut down… One of the reasons I always remembered it was because of one particular feature. Beneath every one of the games it had a “Panic” button. What it did was that when clicked, it would open up to Google in a separate tab. In reality it wouldn’t help much. Because if a teacher just saw you sitting on Google it wouldn’t make them much happier then if you were playing a game… So about a year ago I thought about the idea of making a website where any user could just simple “re-program” the panic button. My idea for the set up is that on the main menu you are faced with a bar where you can enter the URL of a website, the website your supposed to be on. So lets say I’m supposed to be on “science.com” (its a random example) I would enter the URL in the bar on the main menu, then if I click the “Panic” button under any of the games on my website, it will take me in a separate tab to that website that I entered on the main menu. Now I know that in reality this isn’t much better then the other option… More then likely it wouldn’t be used that much but doing little things like this is really the only way I will ever learn PHP. I learn by practice which is where I’m running into the trouble… Because I cant seem to figure out how to do this. So now that I have explained myself I should ask the final questions.

  1. Will cookies carry over from one page on my website to another? Or am I better of going with sessions?
  2. Can the main page remain a HTML document or dose it have to he a PHP file for what I’m doing?
  3. Once I get down taking the users input in a cookie/session, how do I input that data into a link? (Like, how would I get it into this <a href=”users url goes here”> Would that work?

Any other questions I come up with I will be shur to add onto this. Thank you greatly to anyone who can help me out with this. Also I apologize for not displaying any of the code I was working with, I figured it was too broken too be displayed and I was too embarrassed to display it.

You have my gratitude for caring enough to take the time needed to write an excellent post. More people should try to spend that extra time instead of just submitting whatever gibberish they have.

You’re right, HTML (HyperText Markup Language) is what its name suggest, a markup language. Java on the other hand is good knowledge, java is object oriented (OOP = Object Oriented Programming) which lately has found its way into PHP as well. Use it!

Yes it will. When using sessions the user is identified by a cookie as well. PHP automagically sets a “PHPSESSID” cookie with the session id stored on the server. Every time the user revists the page PHP get the cookie, and place the users session data in the $_SESSION array.

On a normal server config you will have to change to .php files. There are plenty of options though, if you can config the server then you can set html to parse php as well, or you can rewrite the URLs so that users see .html while it’s really .php

This would work, please post some code and I/we can get you started :slight_smile:

There are no stupid questions, only stupid answers :slight_smile:

At the beginning of the PHP file.

[php]

<?php setcookie('panic','', time()+10); ?>

[/php]

the form info:

[php]

[/php]

Where im running intro trouble is how to retrieve the users input in the way I was talking about. IE how do I not only fit it in, but how do i retrieve the cookie in the first place?

Thank you very much for replying. (Like i said the code may be a tad off because i was working at it till 1AM last night)

Solution 1: using a cookie

[ul][li]Ending php execution (?>) is not advised if not necessary, so we’ll skip that[/li]
[li]Are you sure you want the cookie to expire after 10 seconds? I’ve changed it to 30 days. [/li]
[li]You are posting data from the form to the php file, so we can receive the data from the global $_POST array[/li][/ul]

[php]<?php
if (!empty($_POST[‘panic’]) {
setcookie(‘panic’,$_POST[‘panic’], time()+606024*30);
}

// you can now get the url from $_COOKIE[‘panic’]
[/php]

Solution 2: using sessions

[ul][li]every php file you have should now start with session_start();[/li]
[li]Ending php execution (?>) is not advised if not necessary, so we’ll skip that[/li]
[li]You are posting data from the form to the php file, so we can receive the data from the global $_POST array[/li][/ul]

[php]<?php
session_start();
if (!empty($_POST[‘panic’]) {
$_SESSION[‘panic’] = $_POST[‘panic’];
}

// you can now get the url from $_SESSION[‘panic’]
[/php]

Thank you greatly. I think ill end up going with the cookies method. But it keeps giving me the error, “Parse error: syntax error, unexpected ‘{’ in C:\xampp\htdocs\index.php on line 2”

yeah I forgot a ) in both examples… sorry ^^

[php] <?php
if (!empty($_POST[‘panic’])) { // added a ) here
setcookie(‘panic’,$_POST[‘panic’], time()+606024*30);
}

// you can now get the url from $_COOKIE[‘panic’][/php]

Do you use an IDE? If not you should consider installing Netbeans :slight_smile:

Thats like a code tester right? Lol i just use Notepad++ and run it through xampp. Oh and for some reason once i put that code in it refuses to let me put the HTML below it… what could cause this?

Nevermind the last part of that reply, i forgot to close the PHP with the ?> haha XD

It’s an editor that’s pretty close to a million times better than Notepad++, you should give it a go. Link in my signature.

Btw: Netbeans is open source and free.

see why you should be using an IDE here
http://www.phphelp.com/forum/the-occasional-tutorial/make-your-life-simpler-use-an-ide!/

now im running into an odd error message. Do you know what this means?
“Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\index.php on line 54” i dont know why it would give me that message since i end the file in HTML…

Please post the code :slight_smile: (full)

And btw your right, netbeans feels much better XD

[php]<?php
if (!empty($_POST[‘panic’])) { // added a ) here
setcookie(‘panic’,$_POST[‘panic’], time()+606024*30);
?>

Main

Welcome!


</head>
<body>

	
	<hr />
	<p>This website is <strong><i>currently</i></strong> under construction!</p>
	<br />
	Here is something I've been working on. Underneath each of the games on my website you will notice a 'Panic!' button. Normally what this button did was take you to Google, in-case your teacher was walking by it would allow you to hop off the website quickly. But now I've added the ability to change where the button takes you. So copy and paste the URL of a website your supposed to be on into the space below!
	<br />
	<form action='index.php' method='post'>
	<input type="text" name="panic" size="80"/>
	<input type="submit" value="Submit"/>
	</form>
	<br />
	<a href="$_COOKIE['panic']">Here!</a>
	<br />
	<h4>In the mean time, here is patrick</h4>
	<iframe width="640" height="480" src="//www.youtube.com/embed/RbYb7rZUz-8" frameborder="0" allowfullscreen></iframe>
	
</body>
<footer><center>
	<hr />
	<!-- START OF HIT COUNTER CODE -->



Free web hostingWeb hosting

</center></footer>[/php]

This is the problem

[php] <?php
if (!empty($_POST[‘panic’])) { // added a ) here
setcookie(‘panic’,$_POST[‘panic’], time()+606024*30);
?>[/php]

You never close the if statement

[php] <?php
if (!empty($_POST[‘panic’])) { // added a ) here
setcookie(‘panic’,$_POST[‘panic’], time()+606024*30);
}
?>[/php]

Now here is where i ran into a problem last time. I cant figure out how to get it to change the damn link! Here is the code i used for that:
[php]<?php
echo ‘Here!’;
?>[/php]

"heres the error it gives me

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404"

Check the output to see which url is actually printed, or just click the link and see where it takes you. It should give a hint of what’s wrong.

Im clicking the link, thats when it brings me to that page. :frowning: and where do i check the output?

on the page with the link or the address bar after clicking the link.

It takes me to localhost/index/$_COOKIE[‘panic’]

And i tried setting it up a different way but it seems to just take me back to the page the link is on… and do nothing els
[php]Here![/php]

First error

[php]<?php
echo ‘Here!’;[/php]

When using single quotes the variables inside will not be parsed. So this will be echoed as the actual string

<a href="$_COOKIE[\'panic\']">Here!</a>

You should fix this using concatenation, the correct way to do this is:
[php]echo ‘Some string ’ . $someVar . ’ more stringy’;[/php]

[php]<?php
echo ‘Here!’;[/php]

Second error

[php]Here![/php]

You don’t do an echo.

Normal echo
[php]Here![/php]

Short echo
[php]Here![/php]

The last method is the cleanest solution IMO

As you suggested i went with the second option but the problem now is that it attempts to take me to, “http://localhost/index/www.google.com” SO how do i declare that its not a URL from my website?

Sponsor our Newsletter | Privacy Policy | Terms of Service